Rnoob here. I have a matrix of zeroes ond ones. I want to delete the rows whose sum of values is not =5, alternatively extract the rows who sum up to 5. Thank you/Mikael -- View this message in context: http://r.789695.n4.nabble.com/delete-rows-whose-sum-is-X-tp3335254p3335254.html Sent from the R help mailing list archive at Nabble.com.
On Fri, Mar 4, 2011 at 8:50 AM, purna <mijony at live.se> wrote:> Rnoob here. > I have a matrix of zeroes ond ones. I want to delete the rows whose sum of > values is not =5, alternatively extract the rows who sum up to 5. > > Thank you/MikaelI think you would greatly benefit from reading some of the intro to R materials that are widely available.> mymat <- matrix(sample(c(1,0), 100, r=TRUE), ncol=10) > rowSums(mymat)[1] 3 4 3 6 3 4 5 7 7 3> mymat[rowSums(mymat) == 5, , drop=FALSE][,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 0 0 1 1 1 0 0 1 1> mymat[rowSums(mymat) != 5, , drop=FALSE][,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 1 1 0 0 0 0 0 1 0 [2,] 0 1 0 1 0 0 0 0 1 1 [3,] 0 0 0 0 1 0 0 1 1 0 [4,] 0 1 0 1 1 1 1 0 0 1 [5,] 1 0 1 0 0 1 0 0 0 0 [6,] 1 1 1 0 0 0 0 1 0 0 [7,] 1 1 0 1 1 1 1 0 1 0 [8,] 1 0 1 1 1 1 1 1 0 0 [9,] 0 0 0 0 0 0 1 0 1 1 Sarah -- Sarah Goslee http://www.functionaldiversity.org
Hi Mikael You really need to provide a reproducible example in the future, it will help people to better understand what you want to do and help you, and help you better understand the answers as well. Try something like this: mat[apply(mat, 1, FUN=function(x) sum(x)=5),] HTH, Ivan Le 3/4/2011 14:50, purna a ?crit :> Rnoob here. > I have a matrix of zeroes ond ones. I want to delete the rows whose sum of > values is not =5, alternatively extract the rows who sum up to 5. > > Thank you/Mikael > > -- > View this message in context: http://r.789695.n4.nabble.com/delete-rows-whose-sum-is-X-tp3335254p3335254.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
Oops, forgot one "=": mat[apply(mat, 1, FUN=function(x) sum(x)==5),] Le 3/4/2011 15:36, Ivan Calandra a ?crit :> Hi Mikael > > You really need to provide a reproducible example in the future, it > will help people to better understand what you want to do and help > you, and help you better understand the answers as well. > > Try something like this: > mat[apply(mat, 1, FUN=function(x) sum(x)=5),] > > HTH, > Ivan > > Le 3/4/2011 14:50, purna a ?crit : >> Rnoob here. >> I have a matrix of zeroes ond ones. I want to delete the rows whose >> sum of >> values is not =5, alternatively extract the rows who sum up to 5. >> >> Thank you/Mikael >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/delete-rows-whose-sum-is-X-tp3335254p3335254.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. >> >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php