[R] generate list of variable names

Erik Iverson eriki at ccbr.umn.edu
Wed Jun 9 18:20:21 CEST 2010

Jon Erik Ween wrote:
> Hi!
> Would anyone know how to generate a list of variable names from a
> data frame by the class of the variable?

a start...

df <- data.frame(f1 = factor(1:10),
                  f2 = factor(1:10),
                  n1 = 1:10,
                  n2 = 1:10)

sapply(df, class)

> I have large tables with different numbers of columns and am trying
> to script some rote analyses. There are several categorizing
> variables (factors) and many response variables (integers and
> numeric). I want to extract a list of classifier column names in one
> list and response variable names in another list, then run for-loops
> to calculate various statistics on the response variables in terms of
> the classifier variables. I thought something like this might work
> (but didn't):

Reproducible example needed.  All this can surely be done more elegantly 
with lapply/mapply instead of for-loops.

More information about the R-help mailing list