[R] new data frame for loop
Tobias Verbeke
tobias.verbeke at gmail.com
Thu Jun 7 20:35:13 CEST 2007
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
More information about the R-help
mailing list