Hi all, Let's say I have a data.frame and wants to turn each of it's columns into a factor. My instinct would be to use as.factor with apply. But this won't work, and result with a data.frame of characters. I found another solution for how to achieve this, but I would also like to understand - *WHY* does it work this way?
Here is an example script: a <- data.frame(x1 = rnorm(100), x2 = sample(c("a","b"), 100, replace = T), x3 = factor(c(rep("a",50) , rep("b",50)))) apply(a2, 2,class) # why is column 3 not a factor ? a[,3] # since it IS a factor. a2 <- apply(a, 2,as.factor) # won't work - why not ? a2[,3] # Why was this just turned into a character ??? # A solution a2 <- lapply(a, as.factor) a3 <- as.data.frame(a2) str(a3) Thanks, Tal ----------------Contact Details:------------------------------------------------------- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.