I need to convert many columns that are numeric to factor type.
An example table:
df <- data.frame(A=1:10, B=2:11, C=3:12)
I tried with apply:
cols<-c('A', 'B')
df[,cols]<-apply(df[,cols], 2, function(x){ as.factor(x)});
But the result is a character class.
> class(df$A)
[1] "character"
How can I do this without doing as.factor for each column?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…