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

S Ellison S.Ellison at lgc.co.uk
Thu Aug 26 18:25:14 CEST 2010


try

lapply(df, class)

Steve E

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

______________________________________________
R-help at 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.

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list