Dear all, I am trying to extract rows from a data.frame based on the rowSums != 0. I want to preserve rownames in the first column in the subset. Does anyone know how to extract all species that don't have rowSums equal to zero? Here it is: # dataset x <- data.frame( species=c("sp.1","sp.2","sp.3","sp.4"), site1=c(2,3,0,0), site2=c(0,0,0,0), site3=c(0,1,0,6), site4=c(0,0,0,0)) #I want extract the matrix: species site1 site2 site3 site4 sp.1 2 0 0 0 sp.2 3 0 1 0 sp.4 0 0 6 0 #extract data.frame of rowSums with x[,2:4] != 0 subset (x, apply (x,1,function(row) all(rowSums(x[,2:4] !=0)) ## don't work Thanks in advance. -- Rog?rio R. Silva MZUSP http://www.mz.usp.br Linux/Debian User # 354364 Linux counter http://counter.li.org
subset(x, rowSums(x[,-1], na.rm=TRUE) != 0) Rogerio Rosa da Silva wrote:> Dear all, > > I am trying to extract rows from a data.frame based on the > rowSums != 0. I want to preserve rownames in the first column in the subset. > > Does anyone know how to extract all species that don't have rowSums equal > to zero? Here it is: > > # dataset > x <- data.frame( > species=c("sp.1","sp.2","sp.3","sp.4"), > site1=c(2,3,0,0), > site2=c(0,0,0,0), > site3=c(0,1,0,6), > site4=c(0,0,0,0)) > > #I want extract the matrix: > > species site1 site2 site3 site4 > sp.1 2 0 0 0 > sp.2 3 0 1 0 > sp.4 0 0 6 0 > > #extract data.frame of rowSums with x[,2:4] != 0 > > subset (x, apply (x,1,function(row) all(rowSums(x[,2:4] !=0)) ## don't work > > > Thanks in advance. >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Something like:> x[rowSums(x[,-1]) > 0,]species site1 site2 site3 site4 1 sp.1 2 0 0 0 2 sp.2 3 0 1 0 4 sp.4 0 0 6 0 Andy> From: Rogerio Rosa da Silva > > Dear all, > > I am trying to extract rows from a data.frame based on the > rowSums != 0. I want to preserve rownames in the first > column in the subset. > > Does anyone know how to extract all species that don't have > rowSums equal > to zero? Here it is: > > # dataset > x <- data.frame( > species=c("sp.1","sp.2","sp.3","sp.4"), > site1=c(2,3,0,0), > site2=c(0,0,0,0), > site3=c(0,1,0,6), > site4=c(0,0,0,0)) > > #I want extract the matrix: > > species site1 site2 site3 site4 > sp.1 2 0 0 0 > sp.2 3 0 1 0 > sp.4 0 0 6 0 > > #extract data.frame of rowSums with x[,2:4] != 0 > > subset (x, apply (x,1,function(row) all(rowSums(x[,2:4] !=0)) > ## don't work > > > Thanks in advance. > > -- > Rog?rio R. Silva > MZUSP http://www.mz.usp.br > Linux/Debian User # 354364 > Linux counter http://counter.li.org > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
you have answer it yourself: x[rowSums(x[,2:4])!=0,] Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Rogerio Rosa da Silva" <rrsilva at ib.usp.br> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, February 09, 2005 2:52 PM Subject: [R] subset> Dear all, > > I am trying to extract rows from a data.frame based on the > rowSums != 0. I want to preserve rownames in the first column in the > subset. > > Does anyone know how to extract all species that don't have rowSums > equal > to zero? Here it is: > > # dataset > x <- data.frame( > species=c("sp.1","sp.2","sp.3","sp.4"), > site1=c(2,3,0,0), > site2=c(0,0,0,0), > site3=c(0,1,0,6), > site4=c(0,0,0,0)) > > #I want extract the matrix: > > species site1 site2 site3 site4 > sp.1 2 0 0 0 > sp.2 3 0 1 0 > sp.4 0 0 6 0 > > #extract data.frame of rowSums with x[,2:4] != 0 > > subset (x, apply (x,1,function(row) all(rowSums(x[,2:4] !=0)) ## > don't work > > > Thanks in advance. > > -- > Rog?rio R. Silva > MZUSP http://www.mz.usp.br > Linux/Debian User # 354364 > Linux counter http://counter.li.org > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Dear Rogerio, x[rowSums(x[,2:5]) != 0,] should do what you want. I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Rogerio Rosa da Silva > Sent: Wednesday, February 09, 2005 8:52 AM > To: r-help at stat.math.ethz.ch > Subject: [R] subset > > Dear all, > > I am trying to extract rows from a data.frame based on the > rowSums != 0. I want to preserve rownames in the first > column in the subset. > > Does anyone know how to extract all species that don't have > rowSums equal to zero? Here it is: > > # dataset > x <- data.frame( > species=c("sp.1","sp.2","sp.3","sp.4"), > site1=c(2,3,0,0), > site2=c(0,0,0,0), > site3=c(0,1,0,6), > site4=c(0,0,0,0)) > > #I want extract the matrix: > > species site1 site2 site3 site4 > sp.1 2 0 0 0 > sp.2 3 0 1 0 > sp.4 0 0 6 0 > > #extract data.frame of rowSums with x[,2:4] != 0 > > subset (x, apply (x,1,function(row) all(rowSums(x[,2:4] !=0)) > ## don't work > > > Thanks in advance. > > -- > Rog?rio R. Silva > MZUSP http://www.mz.usp.br > Linux/Debian User # 354364 > Linux counter http://counter.li.org > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html