[R] extract numerical variables from a data frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 29 11:21:43 CEST 2003

On Fri, 29 Aug 2003, Vincent Spiesser wrote:

> Hi
> I try to create from a data frame a new one which contains only the 
> numerical variables (or factorial ones).
> Is there any function which does this task directly ?
> Or, is there any function which return the mode of each columns of a data 
> frame. ?

I think you want the class, as in

sapply(adf, class)

You could use mode if you really wanted it, but watch out for e.g. the 
mode of a factor being "numeric".

However, for your original question I would use

numeric columns:

DF[sapply(DF, is.numeric)]

factor columns:

DF[sapply(DF, is.factor)]

and similar code is found throughout the R sources.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list