I tried to create a dataframe of two columns with numbers, then create a new column containing the quotient of the two numbers, and then insert the quotient column, like below:
df <- data.frame(a=1:3,b=c(3,NA,5))
df <- sqldf(c("update df set b=4 where b is null", "select * from main.df"),method="raw")
df_quot <- sqldf("select a,a/b[quot] from df")
df <- sqldf("select df_quot.a,b,quot from df, df_quot
where df.a = df_quot.a")
I am trying to do this using one line of code, like something below but fail:
开发者_如何学Cdf <- sqldf("select (select a,a/b[quot] from df) df_quot, a, b from df join df_quot using(a)")
Error in sqliteExecStatement(con, statement, bind.data) :
RS-DBI driver: (error in statement: only a single result allowed for a SELECT that is part of an expression)
What have I done wrong? Thanks.
I think this is one of the possibilities, any other better options?
df <- sqldf("select * from df left join (select a,a/b[quot] from df) df_quot using (a)")
精彩评论