[R] question about split

carslaw david.carslaw at kcl.ac.uk
Fri Jun 17 14:34:17 CEST 2011


Dear R-users

I seem to be stumped on something simple.  I want to split a data frame 
by factor levels given in one or more columns e.g. given

dat <- data.frame(x = runif(100), 
                  fac1 = rep(c("a", "b", "c", "d"), each = 25), 
                  fac2 = rep(c("A", "B"), 50))

I know I can split it by fac1, fac2 by:

split(dat, list(dat$fac1, dat$fac2))

which gives a list of length 8 - as desired.

My question is, given a vector of column name(s) to split by, how can I
supply
split with these? I was thinking something like:

splits <- c("fac1", "fac2")
split(dat, list(splits))

But that is clearly wrong....and I can't see the solution

Many thanks

David Carslaw

Science Policy Group
Environmental Research Group
MRC-HPA Centre for Environment and Health 
King's College London 
Room 4.129 Franklin Wilkins Building 
Stamford Street 
London SE1 9NH 



--
View this message in context: http://r.789695.n4.nabble.com/question-about-split-tp3605281p3605281.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list