Francisco J Molina
2002-Nov-19 21:38 UTC
[R] fast way to check an object is a member of a list?
Is there any fast way to check that an object is a member of a list? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
2002-Nov-20 09:18 UTC
[R] fast way to check an object is a member of a list?
>>>>> "Francisco" == Francisco J Molina <fjmolina at lbl.gov> >>>>> on Tue, 19 Nov 2002 13:38:46 -0800 writes:Francisco> Is there any fast way to check that an object is Francisco> a member of a list? I guess you want to check if a list has a certain *named* component? Assume mylist <- list(ab = .., .....) Then, you can use if(!is.null(mylist $ mycomp)) { .........} or (not quite equivalently!) if("mycomp" %in% names(mylist)) { .........} Does this answer the (little vague) question? Regards, Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ko-Kang Kevin Wang
2002-Nov-20 09:51 UTC
[R] fast way to check an object is a member of a list?
Hi, Is this what you want? > x <- 1:10 > is.list(x) [1] FALSE > foo <- list(x = 1:10, y = 11:20) > is.list(foo) [1] TRUE i.e. use is.list() Cheers, Kevin ------------------------------------------------ Ko-Kang Kevin Wang Post Graduate PGDipSci Student Department of Statistics University of Auckland New Zealand www.stat.auckland.ac.nz/~kwan022 ----- Original Message ----- From: "Francisco J Molina" <fjmolina at lbl.gov> To: "R-help" <r-help at stat.math.ethz.ch> Sent: Wednesday, November 20, 2002 10:38 AM Subject: [R] fast way to check an object is a member of a list?> > Is there any fast way to check that an object is a member of a list? > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> r-help mailing list -- Readhttp://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html> Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Francisco, At 01:38 PM 11/19/2002 -0800, Francisco J Molina wrote:>Is there any fast way to check that an object is a member of a list?Here's a third interpretation of your question, to test whether somewhere is list there is an object identical to y: any(sapply(list, function(x) identical(y, x))) (You could, in addition, test whether there is an element named y in list, as Martin Maechler proposed.) Does that do it? John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
if you are talking about atomic objects, is.element() and %in% may do what you are looking for: > 5 %in% 1:4 [1] FALSE > 3 %in% 1:4 [1] TRUE > is.element(3, as.list(1:4)) [1] TRUE > is.element(5, as.list(1:4)) [1] FALSE > 3 %in% as.list(1:4) [1] TRUE > 5 %in% as.list(1:4) [1] FALSE For general objects (both atomic and non-atomic), I know of no better way than the one already suggested by John Fox: > LIST <- as.list(1:4) > X <- as.integer(3) > any(sapply(LIST, function(y,x) identical(y,x), X)) [1] TRUE > The main problem with using identical() is that it is sensitive to the storage modes, which can lead to unexpected non-matches: > LIST <- as.list(1:4) # elements are integer > X <- 3 # a double value > any(sapply(LIST, function(y,x) identical(y,x), X)) [1] FALSE > identical(X, LIST[[3]]) [1] FALSE > X==LIST[[3]] [1] TRUE > storage.mode(X) [1] "double" > storage.mode(LIST[[3]]) [1] "integer" > hope this helps, tony plate At 01:38 PM 11/19/2002 -0800, Francisco J Molina wrote:>Is there any fast way to check that an object is a member of a list? >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- >r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html >Send "info", "help", or "[un]subscribe" >(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._