Sorry about that -- forgot the case where you might only have a certain entry once: try this,
sapply(split(GS, rownames(GS)), function(x) colSums(as.matrix(x))) or sapply(split(GS, rownames(GS)), function(x) if(is.matrix(x)) colSums(x) else x) I'm not sure if there's a way to force split to give you matrix elements back but it would be nice. Michael PS -- It's nice to cc the list on follow up questions as well so others have a chance to answer and you get a quicker reply (since there are R-helpers in most every timezone, many of whom know much more R-fu than me.) On Wed, Feb 15, 2012 at 4:46 AM, Soheila Khodakarim <lkhodaka...@gmail.com> wrote: > Hi, > > thanks for your guid. > But when I used it I saw this error > > sapply(split(GS,rownames(GS)), colSums) > Error in FUN(X[[1L]], ...) : > 'x' must be an array of at least two dimensions > > Is there any command to do split but keep type of data(Matrix)? > > Regards, > Soheila > > > > > > > > > > > > On Tue, Feb 14, 2012 at 1:12 PM, R. Michael Weylandt > <michael.weyla...@gmail.com> wrote: >> >> 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 >> <lkhodaka...@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@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. > > ______________________________________________ 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.