Hi, my problem is as follows: I have a matrix of two rows like this: 2 2 3 4 4 4 5 5 6 1 1 2 1 3 3 2 1 1 Can I apply something like "group by" in sql? What I want to achieve is the some of second row for each unique entry of first row: 2 -> 2 (=1+1) 3 -> 2 4 -> 7 (=1+3+3) 5 -> 3 (=2+1) 6 -> 1 Thanks!! Henning
Hi Henning, Although there might be a better way to do this, I'd suggest (with my.matrix being the matrix you provided): complete.vector = rep(my.matrix[1,],my.matrix[2,]) ; table(complete.vector) ; Is this what you're looking for? Cheers, Luc Hans-Henning Gabriel wrote:> Hi, > > my problem is as follows: > > I have a matrix of two rows like this: > > 2 2 3 4 4 4 5 5 6 > 1 1 2 1 3 3 2 1 1 > > Can I apply something like "group by" in sql? What I want to achieve > is the some of second row for each unique entry of first row: > > 2 -> 2 (=1+1) > 3 -> 2 > 4 -> 7 (=1+3+3) > 5 -> 3 (=2+1) > 6 -> 1 > > Thanks!! > Henning > > ______________________________________________ > 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.
see ?aggregate m<-rbind(sample(2:6,9,replace=T),sample(1:3,9,replace=T)) aggregate(m[2,],by=list(m[1,]),sum) Hans-Henning Gabriel schrieb:> Hi, > > my problem is as follows: > > I have a matrix of two rows like this: > > 2 2 3 4 4 4 5 5 6 > 1 1 2 1 3 3 2 1 1 > > Can I apply something like "group by" in sql? What I want to achieve > is the some of second row for each unique entry of first row: > > 2 -> 2 (=1+1) > 3 -> 2 > 4 -> 7 (=1+3+3) > 5 -> 3 (=2+1) > 6 -> 1 > > Thanks!! > Henning > > ______________________________________________ > 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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790
rmailbox at justemail.net
2009-Apr-07 17:55 UTC
[R] group by-like statement for 2-row matrix
Another possibility is to transpose and use functions that assume the grouping is by rows, such as summaryBy in the doBy library. If you can easily work with your data this way, the code might be more clear.> library ( doBy ) > databyrows <- rbind ( c ( 2, 2, 3, 4, 4, 4, 5, 5, 6), c(1, 1, 2, 1, 3, 3, 2, 1, 1) ) > databyrows[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 2 2 3 4 4 4 5 5 6 [2,] 1 1 2 1 3 3 2 1 1> databycols <- t ( databyrows) > colnames ( databycols ) <- c("x", "y") > databycols.df <- data.frame (databycols) > # (summaryBy needs a data frame) > # The FUN argument is set explicitly to its default for clarity. > databycols.dfx y 1 2 1 2 2 1 3 3 2 4 4 1 5 4 3 6 4 3 7 5 2 8 5 1 9 6 1> sumbycols.df <- summaryBy ( y ~ x, data = databycols.df , FUN = sum, keep.names = TRUE ) > sumbycols.dfx y 1 2 2 2 3 2 3 4 7 4 5 3 5 6 1> sumbyrows.df <- t ( sumbycols.df ) > sumbyrows.df1 2 3 4 5 x 2 3 4 5 6 y 2 2 7 3 1>----- Original message ----- From: "Eik Vettorazzi" <E.Vettorazzi at uke.uni-hamburg.de> To: "Hans-Henning Gabriel" <hanshenning.gabriel at gmail.com> Cc: r-help at r-project.org Date: Tue, 07 Apr 2009 17:36:45 +0200 Subject: Re: [R] group by-like statement for 2-row matrix see ?aggregate m<-rbind(sample(2:6,9,replace=T),sample(1:3,9,replace=T)) aggregate(m[2,],by=list(m[1,]),sum) Hans-Henning Gabriel schrieb:> Hi, > > my problem is as follows: > > I have a matrix of two rows like this: > > 2 2 3 4 4 4 5 5 6 > 1 1 2 1 3 3 2 1 1 > > Can I apply something like "group by" in sql? What I want to achieve > is the some of second row for each unique entry of first row: > > 2 -> 2 (=1+1) > 3 -> 2 > 4 -> 7 (=1+3+3) > 5 -> 3 (=2+1) > 6 -> 1 > > Thanks!! > Henning > > ______________________________________________ > 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.-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790 ______________________________________________ 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.
Dear Hans, Try also: x <- structure(c(2, 1, 2, 1, 3, 2, 4, 1, 4, 3, 4, 3, 5, 2, 5, 1, 6, 1), .Dim = c(2L, 9L)) tapply(x[2,],x[1,],sum) #2 3 4 5 6 #2 2 7 3 1 HTH, Jorge On Tue, Apr 7, 2009 at 11:06 AM, Hans-Henning Gabriel < hanshenning.gabriel@gmail.com> wrote:> Hi, > > my problem is as follows: > > I have a matrix of two rows like this: > > 2 2 3 4 4 4 5 5 6 > 1 1 2 1 3 3 2 1 1 > > Can I apply something like "group by" in sql? What I want to achieve is the > some of second row for each unique entry of first row: > > 2 -> 2 (=1+1) > 3 -> 2 > 4 -> 7 (=1+3+3) > 5 -> 3 (=2+1) > 6 -> 1 > > Thanks!! > Henning > > ______________________________________________ > 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. >[[alternative HTML version deleted]]