The first code chunks takes multiple column names and loops over them and coerces the columns to factors. This works as expected.
tmp <- data.frame(v1=letters[1:10],v2=letters[1:10],v3=letters[1:10])
vars <- c('v1', 'v2')
tmp[,vars] <- lapply(tmp[,vars], factor)
str(tmp)
Second code chunk isn't generalizing to the special case of a single variable.
tmp <- data.frame(v1=letters[1:10],v2=letters[1:10],v3=letters[1:10])
vars <- c('v1')
tmp[,vars] <- lapply(tmp[,vars], factor)
str(tmp)
Curious if anyone can suggest a good generalizable solution for such situations since the number of variables passed to this piece of code would be unknown in advance in real world.