[R] question

R. Michael Weylandt michael.weylandt at gmail.com
Tue Feb 14 13:12:39 CET 2012


Perhaps something (untested) like

sapply(split(x, rownames(x)), colSums)

Next time it's suggested you use dput() to send your data.

Michael

On Tue, Feb 14, 2012 at 7:08 AM, Soheila Khodakarim
<lkhodakarim at gmail.com> wrote:
> I have this matrix in R,
>
>
>   s1 s2 s3 s4
> g1  1  0  0  0
> g1  0  1  0  0
> g1  0  0  1  0
> g2  1  0  0  0
> g2  0  1  0  0
> g3  1  0  0  0
> g4  0  0  1  0
> g4  1  0  0  0
> g4  0  1  0  0
> g4  0  0  0  1
>
>
> I want to split this matrix based on its rows then sum based on columns,
> finally I want to make this matrix:
>
>   s1 s2 s3 s4
> g1  1  1  1  0
> g2  1  1  0  0
> g3  1  0  0  0
> g4  1  1  1  1
>
> I will appreciate if you guide me.
>
> Regards,
> Soheila
>
>        [[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.



More information about the R-help mailing list