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 3 1 5 7
aa[4]
$`2.2` Houseid Personid Tripid taz 6 2 2 1 58 On Wed, Apr 4, 2012 at 6:29 AM, Rui Barradas <rui1174 at 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.html<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 Barrada
[[alternative HTML version deleted]]
______________________________________________ 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.