Unexpected conversion to chars instead of factors in data frames and matrices
问题 I am not a novice user of R, but the following is most confusing. I have a data frame (although the problem is equally present for matrices) of categorical variables taking the values +1/-1, which I'd like to convert into factors. mat <- matrix(sample(c(-1, +1), 16, replace = T), nrow = 4) mat <- data.frame(mat) However, using mat <- apply(mat, 2, factor) turns integers into characters instead of factors: > mat [,1] [,2] [,3] [,4] [1,] "-1" "1" "-1" "1" [2,] "-1" "-1" "-1" "-1" [3,] "-1" "1"