开发者

generate and insert new column using SQLite

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:

开发者_如何学C
df <- 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)")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜