That's because apply works on arrays/matrices, not data.frames.

It therefore coerces your data.frame to a matrix of type
character, since you have factors, thus the result.

You want sapply or lapply, since a data.frame is actually a
list.

sapply(df, class)

and then to get what you want:

df[sapply(df, is.factor)]

Daniel Brewer wrote:
Hello,

Is there a simple way to get the class type for each column of a
data.frame?  I am in the situation where I would like to get all the
columns of a data.frame that are factors.

I have tried:
apply(df,2,class)
but all the columns come back as class "character".

Thanks

Dan


______________________________________________
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.

Reply via email to