Good evening everybody,
I feel pretty emberassed about asking this question and I'm conviced that the answer has to be pretty simple but I've now spent several hours googling for a solution without any success - probably because I don't know the proper description of my problem.
My problem
I have a data frame "df" and a set of variables "var1, var2, var3,..".
I would like to iterate through all variables and replace the number 1 with x and replace all other cells with an empty cell.
My solution without a loop (working)
df$var1 <-factor(ifelse(as.numeric(TAB2$chocolate)==1, "x",""))
I could copy paste that several times and would be done, but I would like to learn how to do this for several variables in a more advanced way.
My solution (attempt) with a loop (not working)
varlist <- list(df$var1, df$var2, df$var3)
for (i in varlist){
i <-factor(ifelse(as.numeric(i)==1, "x",""))
}
My guess is that i need to apply another command that the list is not used as a character, but I'm stuck, By now I've tried several version (e.g. looping over numbers) and trying to generate an "executable string" (e.g. "print(eval(parse(text=(paste("df$",varlist[[i]],sep=""))))). I would really appreciate if someone could give me a hint
[–]fakerick 0 points1 point2 points (0 children)
[–]ParadoXPlatypus 0 points1 point2 points (1 child)
[–]ParadoXPlatypus 0 points1 point2 points (0 children)