I want to convert variables into factors using apply()
:
a <- data.frame(x1 = rnorm(100),
x2 = sample(c("a","b"), 100, replace = T),
x3 = factor(c(rep("a",50) , rep("b",50))))
a2 <- apply(a, 2,as.factor)
apply(a2, 2,class)
results in:
x1 x2 x3
"character" "character" "character"
I don't understand why this results in character vectors instead of factor vectors.
apply
converts your data.frame to a character matrix. Uselapply
:In second command apply converts result to character matrix, using
lapply
:But for simple lookout you could use
str
:Additional explanation according to comments:
Why does the lapply work while apply doesn't?
The first thing that
apply
does is to convert an argument to a matrix. Soapply(a)
is equivalent toapply(as.matrix(a))
. As you can seestr(as.matrix(a))
gives you:There are no more factors, so
class
return"character"
for all columns.lapply
works on columns so gives you what you want (it does something likeclass(a$column_name)
for each column).You can see in help to
apply
whyapply
andas.factor
doesn't work :Why
sapply
andas.factor
doesn't work you can see in help tosapply
:You never get matrix of factors or data.frame.
How to convert output to
data.frame
?Simple, use
as.data.frame
as you wrote in comment:But if you want to replace selected character columns with
factor
there is a trick:You could use it to replace all columns using: