Christopher Desjardins
2012-Jul-20 00:34 UTC
[R] Changing ungrouped cases to grouped cases
Hi, I have my data the following way: y A B C 0 1 1 2 0 1 2 1 1 1 1 2 0 1 1 2 1 1 1 2 1 1 2 1 0 1 2 2 . . . And so on. How can I make my data look like the following: y A B C 2 1 1 2 1 1 2 1 0 1 2 2 . . . In other words how can I change my ungrouped cases into grouped cases? Thanks! Chris [[alternative HTML version deleted]]
Hi, Try this: dat1<-read.table(text=" y??? A? B? C 0??? 1??? 1? 2 0??? 1??? 2? 1 1??? 1??? 1? 2 0??? 1??? 1? 2 1??? 1??? 1? 2 1??? 1??? 2? 1 0??? 1??? 2? 2 ",sep="",header=TRUE) ?dat2<-aggregate(y~A+B+C,data=dat1,sum) ?dat2<-dat2[,c(4,1:3)] dat3<-dat2[with(dat2,rev(order(y,A,B,C))),] ?dat3 ? y A B C 2 2 1 1 2 1 1 1 2 1 3 0 1 2 2 A.K. ----- Original Message ----- From: Christopher Desjardins <cddesjardins at gmail.com> To: R help <r-help at r-project.org> Cc: Sent: Thursday, July 19, 2012 8:34 PM Subject: [R] Changing ungrouped cases to grouped cases Hi, I have my data the following way: y? ? A? B? C 0? ? 1? ? 1? 2 0? ? 1? ? 2? 1 1? ? 1? ? 1? 2 0? ? 1? ? 1? 2 1? ? 1? ? 1? 2 1? ? 1? ? 2? 1 0? ? 1? ? 2? 2 . . . And so on.? How can I make my data look like the following: y? A? B? C 2? 1? 1? 2 1? 1? 2? 1 0? 1? 2? 2 . . . In other words how can I change my ungrouped cases into grouped cases? Thanks! Chris ??? [[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.
> dtf <- read.table(text="y A B C+ 0 1 1 2 + 0 1 2 1 + 1 1 1 2 + 0 1 1 2 + 1 1 1 2 + 1 1 2 1 + 0 1 2 2", + header=TRUE)> dtagroup <- aggregate(y~A+B+C, dtf, sum)# Gets you the groups. If you need the column/row order:> dtagroup <- dtagroup[order(dtagroup$y, decreasing=TRUE),c(4, 1:3)]---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Christopher Desjardins > Sent: Thursday, July 19, 2012 7:35 PM > To: R help > Subject: [R] Changing ungrouped cases to grouped cases > > Hi, > I have my data the following way: > > y A B C > 0 1 1 2 > 0 1 2 1 > 1 1 1 2 > 0 1 1 2 > 1 1 1 2 > 1 1 2 1 > 0 1 2 2 > . > . > . > And so on. How can I make my data look like the following: > y A B C > 2 1 1 2 > 1 1 2 1 > 0 1 2 2 > . > . > . > > In other words how can I change my ungrouped cases into grouped cases? > Thanks! > Chris > > [[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.