[R] Find classes for each column of a data.frame

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Aug 26 17:39:21 CEST 2010


On Thu, Aug 26, 2010 at 4:31 PM, Daniel Brewer <daniel.brewer at icr.ac.uk> 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".

 apply is treating it as a matrix, and so converts it to the lowest common form.

 try lapply and use is.factor, with some unlist for good measure:

 z=data.frame(x=1:10,l=factor(1:10))
 unlist(lapply(z,is.factor))

   x     l
FALSE  TRUE


Barry



More information about the R-help mailing list