[R] Split data frame based on Class

Kushantha Perera kushanthap at ambaresearch.com
Mon May 18 13:58:47 CEST 2009


Try split()!
?split

e.g
> a
   var1 var2 var3
1     a    1    4
2     a    2    5
3     a    3    6
4     b    4    7
5     b    5    8
6     a    6    9
7     b    7   NA
8     c    8    2
9     c    9    5
10    b    3    4
11    a    2    4
12    a    1    4
13    c    2    3
14    c    5    4
> split(a,a["var1"])
$a
   var1 var2 var3
1     a    1    4
2     a    2    5
3     a    3    6
6     a    6    9
11    a    2    4
12    a    1    4

$b
   var1 var2 var3
4     b    4    7
5     b    5    8
7     b    7   NA
10    b    3    4

$c
   var1 var2 var3
8     c    8    2
9     c    9    5
13    c    2    3
14    c    5    4

Cheers!
Kushantha


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Chris Arthur
Sent: Monday, May 18, 2009 4:54 PM
To: r-help at r-project.org
Subject: [R] Split data frame based on Class

Each row of my data frame is assigned to a class (eg country). Can you 
suggest how I break apart the data frame so that I create new data 
frames for each class

eg

If Class = "US" put in new dataframe dataUS

Thanks in advance for your help

Chris

______________________________________________
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.
This e-mail may contain confidential and/or privileged i...{{dropped:10}}




More information about the R-help mailing list