try this:

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

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


I hope it helps.

Best,
Dimitris


On 6/17/2011 2:34 PM, carslaw wrote:
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.

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


--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/

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

Reply via email to