[R] splitting data frame into fixed rows depending on column rep

Ragia Ibrahim ragia11 at hotmail.com
Sun Dec 6 11:41:16 CET 2015


LOTS OF THANKS

________________________________
> Date: Sun, 6 Dec 2015 21:10:15 +1100 
> Subject: Re: [R] splitting data frame into fixed rows depending on column rep 
> From: drjimlemon at gmail.com 
> To: ragia11 at hotmail.com 
> CC: r-help at r-project.org 
> 
> Hi Ragia, 
> Perhaps the easiest way is to split the data frame into a list by the 
> values of v1: 
> 
> sdf<-split(df,df$v1) 
> 
> Then rename the elements of sdf for convenience: 
> 
> names(sdf)<-paste("v1",1:5,sep="_") 
> 
> Now you can extract whatever you like" 
> 
> sdf$v1_1$v2 
> [1] 3 4 8 
> 
> Of course if you only want the values of v2: 
> 
> split(df$v2,df$v1) 
> $`1` 
> [1] 3 4 8 
> 
> $`2` 
> [1] 9 10 6 
> 
> $`3` 
> [1] 4 5 8 
> 
> $`4` 
> [1] 9 6 8 
> 
> $`5` 
> [1] 4 5 6 
> 
> Jim 
> 
> 
> On Sun, Dec 6, 2015 at 8:55 PM, Ragia Ibrahim 
> <ragia11 at hotmail.com<mailto:ragia11 at hotmail.com>> wrote: 
> Dear group, 
> 
> I have the following data frame 
> 
> v1 v2 v3 v4 v5 
> 1 1 3 1 3.5 1 
> 2 1 4 3 3.5 1 
> 3 1 8 3 3.5 1 
> 4 2 9 8 2.5 1 
> 5 2 10 9 2.5 1 
> 6 2 6 3 1.5 1 
> 7 3 4 3 2.0 1 
> 8 3 5 3 1.0 1 
> 9 3 8 3 1.0 1 
> 10 4 9 8 2.0 1 
> 11 4 6 5 1.0 1 
> 12 4 8 4 1.0 1 
> 13 5 4 3 2.0 1 
> 14 5 5 3 2.0 1 
> 15 5 6 5 2.0 1 
> 
> I'm trying to split it into data frames where each equal v1 so all 
> v1==1 in data frame and all v1==2 in separate data frame and so on. 
> so it would be 
> 
> v1 v2 v3 v4 v5 
> 1 1 3 1 3.5 1 
> 2 1 4 3 3.5 1 
> 3 1 8 3 3.5 1 
> then 
> v1 v2 v3 v4 v5 
> 4 2 9 8 2.5 1 
> 5 2 10 9 2.5 1 
> 6 2 6 3 1.5 1 
> ....... 
> ... 
> and so on 
> 
> I tried 
> a<- split(df, df$v1==1) 
> but the resulting class is a list and when I try to access for example 
> a$v2 it gave me NULL ?? 
> 
> how can I split it an get each vector of v2 for each group, 
> lots of thanks 
> Ragia 
> ______________________________________________ 
> R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To 
> UNSUBSCRIBE and more, see 
> 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. 
> 
 		 	   		  


More information about the R-help mailing list