[R] group by-like statement for 2-row matrix

Luc Villandre villandl at dms.umontreal.ca
Tue Apr 7 17:22:54 CEST 2009


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.




More information about the R-help mailing list