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

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 26 18:00:25 CEST 2010


On Thu, Aug 26, 2010 at 11:31 AM, 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".
>

Try this where iris is a builtin data frame with one several numeric
columns and one factor column, Species:

> onlyFactors <- Filter(is.factor, iris)
> str(onlyFactors)
'data.frame':   150 obs. of  1 variable:
 $ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list