Hi everybody, let us assume i have the following matrixX and vectorY matrixX <- runif(100) dim(matrixX) <- c(10,10) vectorY <- as.matrix(as.character(seq(1,10))) if I define: subsample<-c("2") i can extract the rows from matriX based on the elements in vectorY which are listed in subsample matrixX[vectorY==subsample] if I define subsample with more than 1 element, such as: subsample=c("2", "3") how can i extract the rows from matriX based on the elements in vectorY which are listed in subsample? many thanks Roberto Furlan University of Turin -- ---------------------------------------- La mia Cartella di Posta in Arrivo ? protetta da SPAMfighter 259 messaggi contenenti spam sono stati bloccati con successo. Scarica gratuitamente SPAMfighter!
matrixX[vectorY %in% subsample,] ?"%in%" Roberto Furlan wrote:> Hi everybody, > let us assume i have the following matrixX and vectorY > > matrixX <- runif(100) > dim(matrixX) <- c(10,10) > vectorY <- as.matrix(as.character(seq(1,10))) > > if I define: > subsample<-c("2") > > i can extract the rows from matriX based on the elements in vectorY which > are listed in subsample > matrixX[vectorY==subsample] > > if I define subsample with more than 1 element, such as: > subsample=c("2", "3") > > how can i extract the rows from matriX based on the elements in vectorY > which are listed in subsample? > > many thanks > > Roberto Furlan > University of Turin > > -- > ---------------------------------------- > La mia Cartella di Posta in Arrivo ? protetta da SPAMfighter > 259 messaggi contenenti spam sono stati bloccati con successo. > Scarica gratuitamente SPAMfighter! > > ______________________________________________ > 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 >-- 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
Hi, this should work fine: subset <- c(2,3) for (n in subset) { cat(n,":",matrixX[as.integer(vectorY[row(vectorY)==n]),]) } Good luck Bart Original message: From: Roberto Furlan <roberto.furlan_at_gmail.com> Date: Sun 19 Mar 2006 - 04:47:47 EST Hi everybody, let us assume i have the following matrixX and vectorY matrixX <- runif(100) dim(matrixX) <- c(10,10) vectorY <- as.matrix(as.character(seq(1,10))) if I define: subsample<-c("2") i can extract the rows from matriX based on the elements in vectorY which are listed in subsample matrixX[vectorY==subsample] if I define subsample with more than 1 element, such as: subsample=c("2", "3") how can i extract the rows from matriX based on the elements in vectorY which are listed in subsample? many thanks Roberto Furlan University of Turin [[alternative HTML version deleted]]