Re: [R] Grouping and/or splitting
On 04-04-2012, at 07:15, Ashish Agarwal wrote: Yes. I was missing the DROP argument. But now the problem is splitting is causing some weird ordering of groups. Why weird? See below: DF - read.table(text= Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 3,1,5,7 , header=TRUE, sep=,) aa - split(DF, DF[, 1:2], drop=TRUE) Now the result is aa[3] should is (3,1) and not (2,2). Why? How can I preserve the ascending order? Try this aa[order(names(aa))] Berend aa[3] $`3.1` Houseid Personid Tripid taz 7 31 5 7 aa[4] $`2.2` Houseid Personid Tripid taz 6 22 1 58 On Wed, Apr 4, 2012 at 6:29 AM, Rui Barradas rui1...@sapo.pt wrote: Hello, Ashish Agarwal wrote I have a dataframe imported from csv file below: Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 There are three groups identified based on the combination of first and second columns. How do I split this data frame? I tried aa - split(inpfil, inpfil[,1:2]) but it has problems. Output desired is aa[1] Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 aa[2] Houseid,Personid,Tripid,taz 2,1,1,96 2,1,2,4 2,1,3,2 aa[3] Houseid,Personid,Tripid,taz 2,2,1,58 [[alternative HTML version deleted]] __ R-help@ mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Any of the following three works with me. DF - read.table(text= Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 , header=TRUE, sep=,) DF split(DF, DF[, 1:2], drop=TRUE) split(DF, list(DF$Houseid, DF$Personid), drop=TRUE) with(DF, split(DF, list(Houseid, Personid), drop=TRUE)) The argument 'drop' defaults to FALSE. Was that the problem? Hope this helps, Rui Barrada [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Grouping and/or splitting
Thanks a ton! It was weird because according to me ordering should have by default. Anyways, your workaround along with Weidong's method are both good solutions. On Wed, Apr 4, 2012 at 12:10 PM, Berend Hasselman b...@xs4all.nl wrote: On 04-04-2012, at 07:15, Ashish Agarwal wrote: Yes. I was missing the DROP argument. But now the problem is splitting is causing some weird ordering of groups. Why weird? See below: DF - read.table(text= Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 3,1,5,7 , header=TRUE, sep=,) aa - split(DF, DF[, 1:2], drop=TRUE) Now the result is aa[3] should is (3,1) and not (2,2). Why? How can I preserve the ascending order? Try this aa[order(names(aa))] Berend aa[3] $`3.1` Houseid Personid Tripid taz 7 31 5 7 aa[4] $`2.2` Houseid Personid Tripid taz 6 22 1 58 [[alternative HTML version deleted]] __ R-help@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.
[R] Grouping and/or splitting
I have a dataframe imported from csv file below: Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 There are three groups identified based on the combination of first and second columns. How do I split this data frame? I tried aa - split(inpfil, inpfil[,1:2]) but it has problems. Output desired is aa[1] Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 aa[2] Houseid,Personid,Tripid,taz 2,1,1,96 2,1,2,4 2,1,3,2 aa[3] Houseid,Personid,Tripid,taz 2,2,1,58 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Grouping and/or splitting
how about split(inpfil, paste(inpfil[,1],inpfil[,2],sep=',')) Weidong Gu On Tue, Apr 3, 2012 at 6:42 PM, Ashish Agarwal ashish.agarw...@gmail.com wrote: I have a dataframe imported from csv file below: Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 There are three groups identified based on the combination of first and second columns. How do I split this data frame? I tried aa - split(inpfil, inpfil[,1:2]) but it has problems. Output desired is aa[1] Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 aa[2] Houseid,Personid,Tripid,taz 2,1,1,96 2,1,2,4 2,1,3,2 aa[3] Houseid,Personid,Tripid,taz 2,2,1,58 [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Grouping and/or splitting
Hello, Ashish Agarwal wrote I have a dataframe imported from csv file below: Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 There are three groups identified based on the combination of first and second columns. How do I split this data frame? I tried aa - split(inpfil, inpfil[,1:2]) but it has problems. Output desired is aa[1] Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 aa[2] Houseid,Personid,Tripid,taz 2,1,1,96 2,1,2,4 2,1,3,2 aa[3] Houseid,Personid,Tripid,taz 2,2,1,58 [[alternative HTML version deleted]] __ R-help@ 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. Any of the following three works with me. DF - read.table(text= Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 , header=TRUE, sep=,) DF split(DF, DF[, 1:2], drop=TRUE) split(DF, list(DF$Houseid, DF$Personid), drop=TRUE) with(DF, split(DF, list(Houseid, Personid), drop=TRUE)) The argument 'drop' defaults to FALSE. Was that the problem? Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Grouping-and-or-splitting-tp4530410p4530624.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Grouping and/or splitting
Yes. I was missing the DROP argument. But now the problem is splitting is causing some weird ordering of groups. See below: DF - read.table(text= Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 3,1,5,7 , header=TRUE, sep=,) aa - split(DF, DF[, 1:2], drop=TRUE) Now the result is aa[3] should is (3,1) and not (2,2). Why? How can I preserve the ascending order? aa[3] $`3.1` Houseid Personid Tripid taz 7 31 5 7 aa[4] $`2.2` Houseid Personid Tripid taz 6 22 1 58 On Wed, Apr 4, 2012 at 6:29 AM, Rui Barradas rui1...@sapo.pt wrote: Hello, Ashish Agarwal wrote I have a dataframe imported from csv file below: Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 There are three groups identified based on the combination of first and second columns. How do I split this data frame? I tried aa - split(inpfil, inpfil[,1:2]) but it has problems. Output desired is aa[1] Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 aa[2] Houseid,Personid,Tripid,taz 2,1,1,96 2,1,2,4 2,1,3,2 aa[3] Houseid,Personid,Tripid,taz 2,2,1,58 [[alternative HTML version deleted]] __ R-help@ mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Any of the following three works with me. DF - read.table(text= Houseid,Personid,Tripid,taz 1,1,1,4 1,1,2,7 2,1,1,96 2,1,2,4 2,1,3,2 2,2,1,58 , header=TRUE, sep=,) DF split(DF, DF[, 1:2], drop=TRUE) split(DF, list(DF$Houseid, DF$Personid), drop=TRUE) with(DF, split(DF, list(Houseid, Personid), drop=TRUE)) The argument 'drop' defaults to FALSE. Was that the problem? Hope this helps, Rui Barrada [[alternative HTML version deleted]] __ R-help@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.