Dear R users, I am trying to extract the rownames of a data set for which each columns meet a certain criteria. (condition - elements of each column to be equal 1) I have the correct result, however I am seeking for more efficient (desire vectorization) way in implementing such problem as it can get quite messy if there are hundreds of columns. Arbitrary data set and codes are shown below for your reference: x <- as.data.frame(matrix(round(runif(50),0),nrow=5)) rownames(x) <- letters[1:dim(x)[1]]> xV1 V2 V3 V4 V5 V6 V7 V8 V9 V10 a 0 1 1 1 0 0 0 0 1 0 b 1 1 1 1 0 1 0 0 1 1 c 0 1 1 0 0 0 0 0 0 1 d 1 0 0 1 1 1 1 1 0 0 e 1 0 0 0 0 1 1 0 1 0 V1.ind <- rownames(x)[x[,"V1"]==1] V2.ind <- rownames(x)[x[,"V2"]==1] V3.ind <- rownames(x)[x[,"V3"]==1] V4.ind <- rownames(x)[x[,"V4"]==1] : : V10.ind <- rownames(x)[x[,"V10"]==1]> V1.ind[1] "b" "d" "e"> V2.ind[1] "a" "b" "c"> V3.ind[1] "a" "b" "c" : :> V10.ind[1] "b" "c" Your expertise in resolving this issue would be highly appreciated. Steve [[alternative HTML version deleted]]
Hi, Steven, try lapply( x, function( v) rownames(x)[ v == 1]) or lapply( x, function( v, rn) rn[ v == 1], rn = rownames( x))) which is faster. Regards -- Gerrit --------------------------------------------------------------------- AOR Dr. Gerrit Eichner Mathematical Institute, Room 305 E gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/~gcb7 --------------------------------------------------------------------- On Thu, 27 Aug 2009, Steven Kang wrote:> Dear R users, > > I am trying to extract the rownames of a data set for which each columns > meet a certain criteria. (condition - elements of each column to be equal > 1) > > I have the correct result, however I am seeking for more efficient (desire > vectorization) way in implementing such problem as it can get quite messy > if > there are hundreds of columns. > > Arbitrary data set and codes are shown below for your reference: > > x <- as.data.frame(matrix(round(runif(50),0),nrow=5)) > > rownames(x) <- letters[1:dim(x)[1]] > > > x > V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 > a 0 1 1 1 0 0 0 0 1 0 > b 1 1 1 1 0 1 0 0 1 1 > c 0 1 1 0 0 0 0 0 0 1 > d 1 0 0 1 1 1 1 1 0 0 > e 1 0 0 0 0 1 1 0 1 0 > > V1.ind <- rownames(x)[x[,"V1"]==1] > V2.ind <- rownames(x)[x[,"V2"]==1] > V3.ind <- rownames(x)[x[,"V3"]==1] > V4.ind <- rownames(x)[x[,"V4"]==1] > : > : > V10.ind <- rownames(x)[x[,"V10"]==1] > > > V1.ind > [1] "b" "d" "e" > > V2.ind > [1] "a" "b" "c" > > V3.ind > [1] "a" "b" "c" > : > : > > V10.ind > [1] "b" "c" > > > > Your expertise in resolving this issue would be highly appreciated. > > > Steve > > [[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. >
You can do for (i in 1:ncol(x)) {names <- rownames(x)[which(x[,i]==1)];eval(parse(text=paste("V",i,".ind<-names",sep="")));} --- On Thu, 27/8/09, Steven Kang <stochastickang at gmail.com> wrote:> From: Steven Kang <stochastickang at gmail.com> > Subject: [R] Help on efficiency/vectorization > To: r-help at r-project.org > Received: Thursday, 27 August, 2009, 4:13 PM > Dear R users, > > I am trying to extract the rownames of a data set for which > each columns > meet a certain criteria. (condition - elements of each > column to be equal > 1) > > I have the correct result, however I am seeking for more > efficient (desire > vectorization) way in implementing such problem as it can > get quite messy if > there are hundreds of columns. > > Arbitrary data set and codes are shown below for your > reference: > > x <- as.data.frame(matrix(round(runif(50),0),nrow=5)) > > rownames(x) <- letters[1:dim(x)[1]] > > > x > ? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 > a? 0? 1???1? ? > 1???0???0? ? > 0???0???1? ? 0 > b? 1? 1???1? ? > 1???0???1? ? > 0???0???1? ? 1 > c? 0? 1???1? ? > 0???0???0? ? > 0???0???0? ? 1 > d? 1? 0???0? ? > 1???1???1? ? > 1???1???0? ? 0 > e? 1? 0???0? ? > 0???0???1? ? > 1???0???1? ? 0 > > V1.ind <- rownames(x)[x[,"V1"]==1] > V2.ind <- rownames(x)[x[,"V2"]==1] > V3.ind <- rownames(x)[x[,"V3"]==1] > V4.ind <- rownames(x)[x[,"V4"]==1] > : > : > V10.ind <- rownames(x)[x[,"V10"]==1] > > > V1.ind > [1] "b" "d" "e" > > V2.ind > [1] "a" "b" "c" > > V3.ind > [1] "a" "b" "c" > : > : > > V10.ind > [1] "b" "c" > > > > Your expertise in resolving this issue would be highly > appreciated. > > > Steve > > ??? [[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. >
try this:> x <- as.data.frame(matrix(round(runif(50),0),nrow=5)) > rownames(x) <- letters[1:dim(x)[1]] > xV1 V2 V3 V4 V5 V6 V7 V8 V9 V10 a 0 0 1 0 0 1 0 0 0 1 b 1 0 0 0 1 1 1 1 0 0 c 0 1 0 1 0 0 0 0 1 0 d 0 1 0 0 0 1 0 0 1 1 e 0 0 1 1 0 1 1 0 1 1> lapply(names(x), function(z) rownames(x)[x[[z]] == 1])[[1]] [1] "b" [[2]] [1] "c" "d" [[3]] [1] "a" "e" [[4]] [1] "c" "e" [[5]] [1] "b" [[6]] [1] "a" "b" "d" "e" [[7]] [1] "b" "e" [[8]] [1] "b" [[9]] [1] "c" "d" "e" [[10]] [1] "a" "d" "e">On Thu, Aug 27, 2009 at 2:13 AM, Steven Kang<stochastickang at gmail.com> wrote:> Dear R users, > > I am trying to extract the rownames of a data set for which each columns > meet a certain criteria. (condition - elements of each column to be equal > 1) > > I have the correct result, however I am seeking for more efficient (desire > vectorization) way in implementing such problem as it can get quite messy if > there are hundreds of columns. > > Arbitrary data set and codes are shown below for your reference: > > x <- as.data.frame(matrix(round(runif(50),0),nrow=5)) > > rownames(x) <- letters[1:dim(x)[1]] > >> x > ?V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 > a ?0 ?1 ? 1 ? ?1 ? 0 ? 0 ? ?0 ? 0 ? 1 ? ?0 > b ?1 ?1 ? 1 ? ?1 ? 0 ? 1 ? ?0 ? 0 ? 1 ? ?1 > c ?0 ?1 ? 1 ? ?0 ? 0 ? 0 ? ?0 ? 0 ? 0 ? ?1 > d ?1 ?0 ? 0 ? ?1 ? 1 ? 1 ? ?1 ? 1 ? 0 ? ?0 > e ?1 ?0 ? 0 ? ?0 ? 0 ? 1 ? ?1 ? 0 ? 1 ? ?0 > > V1.ind <- rownames(x)[x[,"V1"]==1] > V2.ind <- rownames(x)[x[,"V2"]==1] > V3.ind <- rownames(x)[x[,"V3"]==1] > V4.ind <- rownames(x)[x[,"V4"]==1] > : > : > V10.ind <- rownames(x)[x[,"V10"]==1] > >> V1.ind > [1] "b" "d" "e" >> V2.ind > [1] "a" "b" "c" >> V3.ind > [1] "a" "b" "c" > : > : >> V10.ind > [1] "b" "c" > > > > Your expertise in resolving this issue would be highly appreciated. > > > Steve > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Try this also: lapply(apply(x == 1, 2, which), names) On Thu, Aug 27, 2009 at 3:13 AM, Steven Kang <stochastickang@gmail.com>wrote:> Dear R users, > > I am trying to extract the rownames of a data set for which each columns > meet a certain criteria. (condition - elements of each column to be equal > 1) > > I have the correct result, however I am seeking for more efficient (desire > vectorization) way in implementing such problem as it can get quite messy > if > there are hundreds of columns. > > Arbitrary data set and codes are shown below for your reference: > > x <- as.data.frame(matrix(round(runif(50),0),nrow=5)) > > rownames(x) <- letters[1:dim(x)[1]] > > > x > V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 > a 0 1 1 1 0 0 0 0 1 0 > b 1 1 1 1 0 1 0 0 1 1 > c 0 1 1 0 0 0 0 0 0 1 > d 1 0 0 1 1 1 1 1 0 0 > e 1 0 0 0 0 1 1 0 1 0 > > V1.ind <- rownames(x)[x[,"V1"]==1] > V2.ind <- rownames(x)[x[,"V2"]==1] > V3.ind <- rownames(x)[x[,"V3"]==1] > V4.ind <- rownames(x)[x[,"V4"]==1] > : > : > V10.ind <- rownames(x)[x[,"V10"]==1] > > > V1.ind > [1] "b" "d" "e" > > V2.ind > [1] "a" "b" "c" > > V3.ind > [1] "a" "b" "c" > : > : > > V10.ind > [1] "b" "c" > > > > Your expertise in resolving this issue would be highly appreciated. > > > Steve > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
>>>>> "MO" == Moshe Olshansky <m_olshansky at yahoo.com> >>>>> on Wed, 26 Aug 2009 23:36:22 -0700 (PDT) writes:MO> You can do MO> for (i in 1:ncol(x)) {names <- rownames(x)[which(x[,i]==1)];eval(parse(text=paste("V",i,".ind<-names",sep="")));} you can, but after install.packages("fortunes") > require("fortunes") > fortune("parse") If the answer is parse() you should usually rethink the question. -- Thomas Lumley R-help (February 2005) So please use one of the other answers given in the thread... MO> --- On Thu, 27/8/09, Steven Kang <stochastickang at gmail.com> wrote: >> From: Steven Kang <stochastickang at gmail.com> >> Subject: [R] Help on efficiency/vectorization >> To: r-help at r-project.org >> Received: Thursday, 27 August, 2009, 4:13 PM >> Dear R users, >> >> I am trying to extract the rownames of a data set for which >> each columns >> meet a certain criteria. (condition - elements of each >> column to be equal >> 1) >> >> I have the correct result, however I am seeking for more >> efficient (desire >> vectorization) way in implementing such problem as it can >> get quite messy if >> there are hundreds of columns. >> >> Arbitrary data set and codes are shown below for your >> reference: >> >> x <- as.data.frame(matrix(round(runif(50),0),nrow=5)) >> >> rownames(x) <- letters[1:dim(x)[1]] >> >> > x >> ? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 >> a? 0? 1???1? ? >> 1???0???0? ? >> 0???0???1? ? 0 >> b? 1? 1???1? ? >> 1???0???1? ? >> 0???0???1? ? 1 >> c? 0? 1???1? ? >> 0???0???0? ? >> 0???0???0? ? 1 >> d? 1? 0???0? ? >> 1???1???1? ? >> 1???1???0? ? 0 >> e? 1? 0???0? ? >> 0???0???1? ? >> 1???0???1? ? 0 >> >> V1.ind <- rownames(x)[x[,"V1"]==1] >> V2.ind <- rownames(x)[x[,"V2"]==1] >> V3.ind <- rownames(x)[x[,"V3"]==1] >> V4.ind <- rownames(x)[x[,"V4"]==1] >> : >> : >> V10.ind <- rownames(x)[x[,"V10"]==1] >> >> > V1.ind >> [1] "b" "d" "e" >> > V2.ind >> [1] "a" "b" "c" >> > V3.ind >> [1] "a" "b" "c" >> : >> : >> > V10.ind >> [1] "b" "c" >> >> >> >> Your expertise in resolving this issue would be highly >> appreciated. >> >> >> Steve >> >> ??? [[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. >> MO> ______________________________________________ MO> R-help at r-project.org mailing list MO> https://stat.ethz.ch/mailman/listinfo/r-help MO> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html MO> and provide commented, minimal, self-contained, reproducible code.