[R] Grouping and/or splitting

Rui Barradas rui1174 at sapo.pt
Wed Apr 4 02:59:31 CEST 2012


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.



More information about the R-help mailing list