Re: [R] new data frame for loop

From: Tobias Verbeke <tobias.verbeke_at_gmail.com>
Date: Thu, 07 Jun 2007 20:35:13 +0200

Hi Emily,

Emily Broccoli wrote:

> I have a data frame with three columns, one coded as a factor. I would like
> to separate my data out into separate data frames according to the factor
> level. Below is a simple example to illustrate. I can get R to return the
> data in the correct format but cannot work out how to get separate data
> frames. I am a newcommer to programming in R so am not sure what I am
> missing! Thanks, Emily
>
> a<-seq(1,20, by=2)
> b<-seq(1,30, by=3)
> ID<-as.factor(c(1,1,1,2,2,2,3,3,3,3))
> df<-data.frame(a,b,ID)

The function split will give you a list
of data frames split according to a factor:

 > split(df, df$ID)
$`1`

   a b ID
1 1 1 1
2 3 4 1
3 5 7 1

$`2`

    a b ID
4 7 10 2
5 9 13 2
6 11 16 2

$`3`

     a b ID
7 13 19 3
8 15 22 3
9 17 25 3
10 19 28 3

See ?split.

HTH,
Tobias

-- 

Tobias Verbeke - Consultant
Business & Decision Benelux
Rue de la révolution 8
1000 Brussels - BELGIUM

+32 499 36 33 15
tobias.verbeke_at_businessdecision.com

______________________________________________
R-help_at_stat.math.ethz.ch 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.
Received on Thu 07 Jun 2007 - 18:39:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Jun 2007 - 20:33:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.