I have included a lot of sql code in R using the sqldf
library. Since this is becoming increasingly hard to digest, I would like to do it using, for example dplyr
in native R.
How would I do it for the following:
select v1, v2 (case when q1 > 0 then -q1 else q1 end) as v3 from dataset
Advertisement
Answer
Maybe something like this in dplyr
:
library(dplyr) dataset %>% mutate(v3 = ifelse(q1>0, -q1, q1)) %>% select(v1, v2, v3)
Or base R:
dataset$v3 <- ifelse(dataset$q1 > 0,-dataset$q1, dataset$q1) dataset[c("v1","v2","v3")]