I have already encountered this problem several times, and this time I really want to get an effective solution. The main problem is that I want to iterate over a list of variables using the paste function.
dat <- read.csv("some file", header=TRUE) list.R <- c("IC","IG","DM","IM","IN","EN","RM") for (RO in list.R){ paste("dat$",RO,"_I", sep="")[ paste("dat$",RO,"_I", sep="") == "Strongly disagree"] <- 1 }
I inserted the variable name together, but that gives me a string in quotation marks for blocks. I tried the following but nothing worked:
eval(parse(text=paste("dat$",RO,"_I", sep="")))
or
get(paste("dat$",RO,"_I", sep=""))
Do you know how to solve this to make the loop work? I would really appreciate your help :)
(I know that in this case I could also use as.numeric(levels(dat$IC_I))[dat$IC_I] but the order of the levels is incorrect)
source share