Gundala Viswanath
2009-Jan-13 02:41 UTC
[R] Returning Non-Unique Index with Which (alternatives?)
Dear all, I tried to find index in repo given a query with this:> repo <- c("AAA", "AAT", "AAC", "AAG", "ATA", "ATT") > qr <- c("AAC", "ATT", "ATT") > which(repo%in%qr)[1] 3 6 Note that the query contain repeating elements, yet the output of which only returns unique. How can I make it returning [1] 3 6 6 instead? - Gundala Viswanath Jakarta - Indonesia
Hi Gundala, The following should work. x <- numeric(length(qr)) for(k in 1:3) x[k] <- which(repo %in% qr[k]) also, be careful about overwriting qr - it is an base package function. regards, Andrew On Jan 13, 1:41?pm, "Gundala Viswanath" <gunda... at gmail.com> wrote:> Dear all, > > I tried to find index in repo given ?a query with this: > > > repo <- c("AAA", "AAT", "AAC", "AAG", "ATA", "ATT") > > qr <- c("AAC", "ATT", "ATT") > > which(repo%in%qr) > > [1] 3 6 > > Note that the query contain repeating elements, yet > the output of which only returns unique. > > How can I make it returning > > [1] 3 6 6 > > instead? > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
David Winsemius
2009-Jan-13 03:34 UTC
[R] Returning Non-Unique Index with Which (alternatives?)
> sapply(qr, function (x) which(repo %in% x)+ ) AAC ATT ATT 3 6 6 On Jan 12, 2009, at 9:41 PM, Gundala Viswanath wrote:> Dear all, > > > I tried to find index in repo given a query with this: > >> repo <- c("AAA", "AAT", "AAC", "AAG", "ATA", "ATT") >> qr <- c("AAC", "ATT", "ATT") >> which(repo%in%qr) > [1] 3 6 > > > Note that the query contain repeating elements, yet > the output of which only returns unique. > > How can I make it returning > > [1] 3 6 6 > > instead? > > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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.
Jorge Ivan Velez
2009-Jan-13 05:16 UTC
[R] Returning Non-Unique Index with Which (alternatives?)
Dear Gundala, As Jim Holtman suggested in a previous post, match() should do the job: repo <- c("AAA", "AAT", "AAC", "AAG", "ATA", "ATT") qr <- c("AAC", "ATT", "ATT") match(qr,repo) [1] 3 6 6 HTH, Jorge On Mon, Jan 12, 2009 at 9:41 PM, Gundala Viswanath <gundalav@gmail.com>wrote:> Dear all, > > > I tried to find index in repo given a query with this: > > > repo <- c("AAA", "AAT", "AAC", "AAG", "ATA", "ATT") > > qr <- c("AAC", "ATT", "ATT") > > which(repo%in%qr) > [1] 3 6 > > > Note that the query contain repeating elements, yet > the output of which only returns unique. > > How can I make it returning > > [1] 3 6 6 > > instead? > > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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. >[[alternative HTML version deleted]]