0

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.

dhc
  • 300
  • 2
  • 8
  • 1
    If you do `lapply(tmp[,vars, drop = F], factor)` it should work...with a single column specified, unless you set `drop = F` the result will be coerced to the lowest possible dimension when extracting... – Ben May 22 '22 at 01:37

0 Answers0