Hello, Here's my problem. I have a large data frame and a vector with some of its row names. I'd like to have a new data frame only with those rows that match this vector of row names. I tried this: data<-cbind(c(1,2,3,4,5,6),c(2,3,4,5,6,7)) rownames(data)<-c("a", "b", "c","d","e","f") names.to.keep<-c("a", "c", "d") rows.to.keep<-which(rownames(data)==names.to.keep) But it didn't work. Any suggestions? thanks a lot. Jonathan. [[alternative HTML version deleted]]
Jonathan - To make your approach work, you'd need to replace =with %in%:> rows.to.keep<-which(rownames(data) %in% names.to.keep)[1] 1 3 4 But to answer you're original question, remember that the point of rownames is that they can be used to index a data frame:> data[rows.to.keep,][,1] [,2] a 1 2 c 3 4 d 4 5 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Fri, 14 Jan 2011, Jonathan Hughes wrote:> > > Hello, > > Here's my problem. I have a large data frame and a vector with some of its row names. I'd like to have a new data frame only with those rows that match this vector of row names. > > I tried this: > > data<-cbind(c(1,2,3,4,5,6),c(2,3,4,5,6,7)) > rownames(data)<-c("a", "b", "c","d","e","f") > names.to.keep<-c("a", "c", "d") > rows.to.keep<-which(rownames(data)==names.to.keep) > > But it didn't work. Any suggestions? > > thanks a lot. > > Jonathan. > > [[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. >
ANJAN PURKAYASTHA
2011-Jan-14 18:49 UTC
[R] filtering a dataframe with a vector of rownames
subset(data, row.names(data) %in% name.to.keep HTH, Anjan On Fri, Jan 14, 2011 at 1:25 PM, Jonathan Hughes < jonathan.hughes.10@live.com> wrote:> > > Hello, > > Here's my problem. I have a large data frame and a vector with some of its > row names. I'd like to have a new data frame only with those rows that match > this vector of row names. > > I tried this: > > data<-cbind(c(1,2,3,4,5,6),c(2,3,4,5,6,7)) > rownames(data)<-c("a", "b", "c","d","e","f") > names.to.keep<-c("a", "c", "d") > rows.to.keep<-which(rownames(data)==names.to.keep) > > But it didn't work. Any suggestions? > > thanks a lot. > > Jonathan. > > [[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. >-- ==================================anjan purkayastha, phd. research associate fas center for systems biology, harvard university 52 oxford street cambridge ma 02138 phone-703.740.6939 ================================== [[alternative HTML version deleted]]
On 14.01.2011 19:25, Jonathan Hughes wrote:> > > Hello, > > Here's my problem. I have a large data frame and a vector with some of its row names. I'd like to have a new data frame only with those rows that match this vector of row names. > > I tried this: > > data<-cbind(c(1,2,3,4,5,6),c(2,3,4,5,6,7)) > rownames(data)<-c("a", "b", "c","d","e","f") > names.to.keep<-c("a", "c", "d") > rows.to.keep<-which(rownames(data)==names.to.keep)replace the last line by rows.to.keep <- data[names.to.keep,] and please read the docs on indexing. Best, Uwe Ligges> But it didn't work. Any suggestions? > > thanks a lot. > > Jonathan. > > [[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.