[R] help about loop

韩文衡 hwh2222 at 163.com
Sat Feb 19 05:06:59 CET 2011


Hi all,
I am try to do a loop  on a data .such as:

> X<-data.frame(name=c(1:9),SN=c(1,1,1,2,2,2,3,3,3),EW=rep(1:3,3))
> X
   name SN EW
1    1  1  1
2    2  1  2
3    3  1  3
4    4  2  1
5    5  2  2
6    6  2  3
7    7  3  1
8    8  3  2
9    9  3  3


> out<-list()
> for ( i in 1:3) {out[[i]]<-subset( X,X$SN==1&EW==i) }  ##this expression  
> can have the subset of EW=1 (and EW=2 EW=3) when SN=1
> out
[[1]]
   name SN EW
1    1  1  1

[[2]]
   name SN EW
2    2  1  2

[[3]]
   name SN EW
3    3  1  3

>

Question:  What can I do if I want to have the all subset at once  when  
SN=1:3 ? How to write the loop?

thank you very much!

----hanwenheng



More information about the R-help mailing list