I would like to create a certain number of dataframes out of one dataframe where each of the dataframes is related to a factor. This should be possible with a loop or a function, as is very clumsy to do it manually when there are quite a lot factors. For example having a dataframe called "exmpl": Site Value 1 12 1 15 1 18 1 21 1 12 1 13 2 15 2 12 2 58 2 62 2 22 2 65 2 29 3 21 3 55 3 11 3 98 I would like to create 3 dataframes, where "exmpl_01" equals: 1 12 1 15 1 18 1 21 1 12 1 13 "exmpl_02" equals: 2 15 2 12 2 58 2 62 2 22 2 65 2 29 "exmpl_03" equals: 3 21 3 55 3 11 3 98 Thanks for any help. Walter -- View this message in context: http://r.789695.n4.nabble.com/Creating-dataframes-tp3520509p3520509.html Sent from the R help mailing list archive at Nabble.com.
The way to do what you want is to use the split function, for
example,
mydataframes = split(exmpl,exmpl$Site)
This will return a list with all the data frames.
If you don't understand why this is a better solution than
creating many separate data frames, let us know what you plan
to do with the data frames you're creating.
- Phil
On Fri, 13 May 2011, Woida71 wrote:
> I would like to create a certain number of dataframes out of one dataframe
> where each of the dataframes
> is related to a factor. This should be possible with a loop or a function,
> as is very clumsy to do it manually
> when there are quite a lot factors.
>
> For example having a dataframe called "exmpl":
>
> Site Value
> 1 12
> 1 15
> 1 18
> 1 21
> 1 12
> 1 13
> 2 15
> 2 12
> 2 58
> 2 62
> 2 22
> 2 65
> 2 29
> 3 21
> 3 55
> 3 11
> 3 98
>
> I would like to create 3 dataframes, where
> "exmpl_01" equals:
> 1 12
> 1 15
> 1 18
> 1 21
> 1 12
> 1 13
>
> "exmpl_02" equals:
> 2 15
> 2 12
> 2 58
> 2 62
> 2 22
> 2 65
> 2 29
>
> "exmpl_03" equals:
> 3 21
> 3 55
> 3 11
> 3 98
>
> Thanks for any help.
> Walter
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Creating-dataframes-tp3520509p3520509.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>
On May 13, 2011, at 10:28 AM, Woida71 wrote:> I would like to create a certain number of dataframes out of one dataframe > where each of the dataframes > is related to a factor. This should be possible with a loop or a function, > as is very clumsy to do it manually > when there are quite a lot factors. > > For example having a dataframe called "exmpl": > > Site Value > 1 12 > 1 15 > 1 18 > 1 21 > 1 12 > 1 13 > 2 15 > 2 12 > 2 58 > 2 62 > 2 22 > 2 65 > 2 29 > 3 21 > 3 55 > 3 11 > 3 98 > > I would like to create 3 dataframes, where > "exmpl_01" equals: > 1 12 > 1 15 > 1 18 > 1 21 > 1 12 > 1 13 > > "exmpl_02" equals: > 2 15 > 2 12 > 2 58 > 2 62 > 2 22 > 2 65 > 2 29 > > "exmpl_03" equals: > 3 21 > 3 55 > 3 11 > 3 98 > > Thanks for any help. > WalterThe easiest way is to create a list of data frames using:> DFSite Value 1 1 12 2 1 15 3 1 18 4 1 21 5 1 12 6 1 13 7 2 15 8 2 12 9 2 58 10 2 62 11 2 22 12 2 65 13 2 29 14 3 21 15 3 55 16 3 11 17 3 98> split(DF, DF$Site)$`1` Site Value 1 1 12 2 1 15 3 1 18 4 1 21 5 1 12 6 1 13 $`2` Site Value 7 2 15 8 2 12 9 2 58 10 2 62 11 2 22 12 2 65 13 2 29 $`3` Site Value 14 3 21 15 3 55 16 3 11 17 3 98 See ?split for more information. HTH, Marc Schwartz