I am a Stata user trying to switch to R and having the usual beginner's struggle. I have been trying (and failing) to do a loop for a few days and I now surrender. What I want to do (in a loop):
start from a list of variable names
create a new variable
recode that new variable(s) based on the value of existing variables
possibly do so using the dplyr syntax, but this is not essential, only for consistency with the rest of my code.
Here is a stylised example of what I am trying to do. In my actual data, the x.x and x.y variables originate from the join function applied to 2 existing data frames.
N <- 1000
df <- data.frame(x1 = rnorm(N),
x2.x = rnorm(N)+2,x2.y = rnorm(N)-2,
x3.x = rnorm(N)+3,x3.y = rnorm(N)-3)
varlist <- c("x2","x3")
lapply(varlist, function(x) {
df <- df %>% mutate(x = ifelse(x1 < 0, paste0(x,".y"),paste0(x,".x")) # generate varialble "x" values from existing x.x and x.y
})
When I run the lapply part of the code I get the error message
Error: unexpected '}' in: " df <- df %>% mutate(x = ifelse(x1 < 0, paste0(x,".y"),paste0(x,".x")) # generate varialble "x" values from existing x.x and x.y }"
even though it should be expected... I am sure there a number of mistakes in my code, and that's partly because I am used to macros in Stata for which there is no direct equivalent in R. Anyway, if you can point me in the right direction it would be fantastic!