I am working with a data set and I need to run several queries one after the other in order to create some new variables. The problem is that after the first query, the new variable which is created is of class data.frame. This means that subsequent queries do not run, because as explained in the following post: R- sqldf error raw vs double ... "The columns of your data.frame cannot be of class data.frame for sqldf to work"
The problem is that the above post does not tell me how to solve this problem. You can replicate my problem with the following example:
set.seed(999)
dt <- data.frame("x"=rnorm(150, 600,195))
sapply(dt, class)
library(sqldf)
dt$x2 <- sqldf("SELECT CASE
WHEN x>999 THEN x / 100
WHEN x>99 THEN x/10
ELSE x
END as x2
FROM dt")
sapply(dt, class)
The output of the last line shows that the class of X2 is data.frame. This means that when I try to run my next query, it will not work.
dt$x3 <- sqldf("SELECT CASE
WHEN x>999 THEN x - 100
WHEN x>99 THEN x - 10
ELSE x + 10
END as x3
FROM dt")
Any suggestion on how to resolve this?