[R] split data set

Chuck Cleland ccleland at optonline.net
Fri Jan 19 20:20:38 CET 2007


Aimin Yan wrote:
> I have a data(ABC) like this:
> 
>       x   y
> A   3   4
> A   1   3
> B   2   6
> B   4   8
> C   5   4
> C   6   7
> 
> I want to split this data into
> 
> A:
>    x   y
> A   3   4
> A   1   3
> 
> B
> B   2   6
> B   4   8
> 
> C
> C   5   4
> C   6   7
> 
> anyone knows how to do that?

?split

> df <- data.frame(L = rep(c("A","B","C"), each =2), x = runif(6),
y=runif(6))

> df
  L          x          y
1 A 0.57411628 0.01975706
2 A 0.21079282 0.62153084
3 B 0.46798592 0.97374423
4 B 0.08731642 0.33845989
5 C 0.08801709 0.93843814
6 C 0.54576658 0.26798882

> split(df, df$L)
$A
  L         x          y
1 A 0.5741163 0.01975706
2 A 0.2107928 0.62153084

$B
  L          x         y
3 B 0.46798592 0.9737442
4 B 0.08731642 0.3384599

$C
  L          x         y
5 C 0.08801709 0.9384381
6 C 0.54576658 0.2679888

> thanks,
> 
> Aimin Yan
> 
> ______________________________________________
> 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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list