Suppose there are many data frames that need the same operation performed on them. For example:
prefix <- c("Mrs.","Mrs.","Mr","Dr.","Mrs.","Mr.","Mrs.","Ms","Ms","Mr")
measure <- rnorm(10)
df1 <- data.frame(prefix,measure)
df1$gender[df1$prefix=="Mrs."] <- "F"
Would create an indicator variable called gender when the value in the adjacent row was "Mrs.". A general way to loop over string variables in R was adapted from here with the function as.name()
added to remove the quotes from "i":
dflist <- c("df1","df2","df3","df4","df5")
for (i in dflist) {
as.name(i)$gender[as.name(i)$prefix=="Ms."] <- "F"
}
Unfortunately this doesn't work. Any suggestions?