I want just to convert two columns of a data frame to factors. I use the apply function but the result is characters, not factors. Any idea what I am doing wrong ?
aa <- c(1,2,3,4)
bb <- c(6,7,8,9)
xx <- data.frame(aa, bb)
xx
yy <- apply(xx, 2, function(xx) as.factor(xx))
# aa bb
# [1,] "1" "6"
# [2,] "2" "7"
# [3,] "3" "8"
# [4,] "4" "9"
When I am implementing the same to a stand alone vector, it works:
nn <- c(1,2,3,4)
mm <- as.factor(nn)
mm