Eric Rupley
2013-Mar-28 07:40 UTC
[R] how to search a list that contains multiple dissimilar vectors?
Dear All, This is a simple question, but I'm stumped about the simplest way to search a list object such as the following: This randomish snippet: n <- c(round(runif(round(runif(1,1,10),0),1,10),0)) alist <- new("list") for (i in seq_along(n)) { alist[[i]] <- c(round(runif(round(runif(1,1,10),0),1,10),0)) } names(alist) <- sample(letters[1:length(n)]) rm(n);c(alist) ...produces something like this: $d [1] 4 $b [1] 3 5 3 $a [1] 2 5 7 3 10 3 4 9 9 $c [1] 6 3 7 4 5 10 8 10 3 My question is how does one search the list for a given value, in a most compressed set of commands, in order two return two separate indices: a) the index of the list element(s) containing the value, and b) the index of the matching value(s) within the vector. Right now, I'm writing cumbersome loops to iterate though the elements, but there must be a simple, effective method to which I have not found a reference. Many thanks in advance, and apologies if I have overlooked a reference passage. Best, Eric -- Eric Rupley University of Michigan, Museum of Anthropology 1109 Geddes Ave, Rm. 4013 Ann Arbor, MI 48109-1079
Eik Vettorazzi
2013-Mar-28 08:21 UTC
[R] how to search a list that contains multiple dissimilar vectors?
Hi Eric, something like the following might me a starter (index<-lapply(alist,function(x)which(x==x.search))) cheers. Am 28.03.2013 08:40, schrieb Eric Rupley:> > Dear All, > > This is a simple question, but I'm stumped about the simplest way to search a list object such as the following: > > This randomish snippet: > > n <- c(round(runif(round(runif(1,1,10),0),1,10),0)) > alist <- new("list") > for (i in seq_along(n)) { > alist[[i]] <- c(round(runif(round(runif(1,1,10),0),1,10),0)) > } > names(alist) <- sample(letters[1:length(n)]) > rm(n);c(alist) > > > ...produces something like this: > > $d > [1] 4 > > $b > [1] 3 5 3 > > $a > [1] 2 5 7 3 10 3 4 9 9 > > $c > [1] 6 3 7 4 5 10 8 10 3 > > My question is how does one search the list for a given value, in a most compressed set of commands, in order two return two separate indices: a) the index of the list element(s) containing the value, and b) the index of the matching value(s) within the vector. > > Right now, I'm writing cumbersome loops to iterate though the elements, but there must be a simple, effective method to which I have not found a reference. > > Many thanks in advance, and apologies if I have overlooked a reference passage. > Best, > Eric > > -- > Eric Rupley > University of Michigan, Museum of Anthropology > 1109 Geddes Ave, Rm. 4013 > Ann Arbor, MI 48109-1079 > > > ______________________________________________ > 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. >-- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Martin Zeitz (Vorsitzender), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
Gerrit Eichner
2013-Mar-28 08:29 UTC
[R] how to search a list that contains multiple dissimilarvectors?
Hi, Eric, as a quick hack, does spots <- sapply( alist, function( listcomp) which( listcomp == value.to.look.for) ) spots[ sapply( spots, length) == 0] <- NULL do what you want? Regards -- Gerrit On Thu, 28 Mar 2013, Eric Rupley wrote:> > Dear All, > > This is a simple question, but I'm stumped about the simplest way to > search a list object such as the following: > > This randomish snippet: > > n <- c(round(runif(round(runif(1,1,10),0),1,10),0)) > alist <- new("list") > for (i in seq_along(n)) { > alist[[i]] <- c(round(runif(round(runif(1,1,10),0),1,10),0)) > } > names(alist) <- sample(letters[1:length(n)]) > rm(n);c(alist) > > > ...produces something like this: > > $d > [1] 4 > > $b > [1] 3 5 3 > > $a > [1] 2 5 7 3 10 3 4 9 9 > > $c > [1] 6 3 7 4 5 10 8 10 3 > > My question is how does one search the list for a given value, in a most compressed set of commands, in order two return two separate indices: a) the index of the list element(s) containing the value, and b) the index of the matching value(s) within the vector. > > Right now, I'm writing cumbersome loops to iterate though the elements, but there must be a simple, effective method to which I have not found a reference. > > Many thanks in advance, and apologies if I have overlooked a reference passage. > Best, > Eric > > -- > Eric Rupley > University of Michigan, Museum of Anthropology > 1109 Geddes Ave, Rm. 4013 > Ann Arbor, MI 48109-1079 > > > ______________________________________________ > 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. > >