Hi List, I'm trying to work out how to use which(), or another function, to find the top-level index of a list item based on a condition. An example will clarify my question. a <- list(c(1,2),c(3,4)) a [[1]] [1] 1 2 [[2]] [1] 3 4 I want to find the top level index of c(1,2), which should return 1 since; a[[1]] [1] 1 2 I can't seem to work out the syntax. I've tried; which(a == c(1,2)) and an error about coercing to double is returned. I can find the index of elements of a particular item by which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 respectively as they should. Any thoughts? C [[alternative HTML version deleted]]
Chris, Well, the 'answer' could be: which(sapply(a, function(x) all(x == c(1,2)))) But I wonder how these elements of 'a' in your actual application are coming to be? If you're constructing them, you can give the elements of the list names, and then it doesn't matter what numerical index they have, you can just reference them by name. a <- list(name1 = 1:2, name2 = 3:4) a a <- c(anothername = list(9:10), a) a a$name1 Chris Carleton wrote:> Hi List, > > I'm trying to work out how to use which(), or another function, to find the > top-level index of a list item based on a condition. An example will clarify > my question. > > a <- list(c(1,2),c(3,4)) > a > [[1]] > [1] 1 2 > > [[2]] > [1] 3 4 > > I want to find the top level index of c(1,2), which should return 1 since; > > a[[1]] > [1] 1 2 > > I can't seem to work out the syntax. I've tried; > > which(a == c(1,2)) > > and an error about coercing to double is returned. I can find the index of > elements of a particular item by > > which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 > respectively as they should. Any thoughts? > > C > > [[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.
Hi Chris, Does this do what you're after? It just compares each element of a (i.e., a[[1]] and a[[2]]) to c(1, 2) and determines if they are identical or not. which(sapply(a, identical, y = c(1, 2))) There were too many 1s floating around for me to figure out if you wanted to find elements of a that matched the entire vector or subelements of a that matched elements of the vector (if that makes any sense). HTH, Josh On Mon, Nov 15, 2010 at 1:24 PM, Chris Carleton <w_chris_carleton at hotmail.com> wrote:> Hi List, > > I'm trying to work out how to use which(), or another function, to find the > top-level index of a list item based on a condition. An example will clarify > my question. > > a <- list(c(1,2),c(3,4)) > a > [[1]] > [1] 1 2 > > [[2]] > [1] 3 4 > > I want to find the top level index of c(1,2), which should return 1 since; > > a[[1]] > [1] 1 2 > > I can't seem to work out the syntax. I've tried; > > which(a == c(1,2)) > > and an error about coercing to double is returned. I can find the index of > elements of a particular item by > > which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 and [1] 1 > respectively as they should. Any thoughts? > > C > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
On Nov 15, 2010, at 4:24 PM, Chris Carleton wrote:> Hi List, > > I'm trying to work out how to use which(), or another function, to > find the > top-level index of a list item based on a condition. An example will > clarify > my question. > > a <- list(c(1,2),c(3,4)) > a > [[1]] > [1] 1 2 > > [[2]] > [1] 3 4 > > I want to find the top level index of c(1,2), which should return 1 > since; > > a[[1]] > [1] 1 2 > > I can't seem to work out the syntax. I've tried; > > which(a == c(1,2))It's a bit more involved than that (since which is expecting a vector of logicals and mapply is expecting a set of list arguments.) I needed to send mapply a MoreArgs list that would remain constant from test to test when using identical. > which(mapply(identical, a, MoreArgs=list(c(1,2)))) [1] 1 (Admittedly very similar to Iverson's solution, but it is more readable to my eyes. On the other hand my method may stumble on more complex object with attributes. You should read hte identical help page at any rate.)> > and an error about coercing to double is returned. I can find the > index of > elements of a particular item by > > which(a[[1]]==c(1,2)) or which(a[[1]]==1) etc that return [1] 1 2 > and [1] 1 > respectively as they should.But neither was testing the "overall equality" of a[1]'s contents with c(1,2) which appears to be your goal. Iverson's "all" and my "identical" accomplish that goal.> Any thoughts? > > C-- David Winsemius, MD West Hartford, CT