Dear R-users, in a minimal example exists() gives FALSE on an object which obviously does exist. How can I check on that list object anyway else, please?> SmoothData <- list(exists=TRUE, span=0.001) > SmoothData$exists [1] TRUE $span [1] 0.001> exists("SmoothData")TRUE> exists("SmoothData$span")FALSE> exists("SmoothData[[2]]")FALSE Thank you for any opinion regarding this topic. Zroutik [[alternative HTML version deleted]]
SmoothData$span is not an object which can be checked by exists(), but part of an object which can be checked by is.null(). On Wed, May 20, 2009 at 12:07 AM, ?rout?k <zroutik at gmail.com> wrote:> Dear R-users, > > in a minimal example exists() gives FALSE on an object which obviously does > exist. How can I check on that list object anyway else, please? > >> SmoothData <- list(exists=TRUE, span=0.001) >> SmoothData > $exists > [1] TRUE > > $span > [1] 0.001 > >> exists("SmoothData") > TRUE > >> exists("SmoothData$span") > FALSE > >> exists("SmoothData[[2]]") > FALSE > > Thank you for any opinion regarding this topic. > Zroutik > > ? ? ? ?[[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. >
Wacek Kusnierczyk
2009-May-19 16:30 UTC
[R] exists function on list objects gives always a FALSE
?rout?k wrote:> >> SmoothData <- list(exists=TRUE, span=0.001) >> SmoothData >> > $exists > [1] TRUE > > $span > [1] 0.001 > > >> exists("SmoothData") >> > TRUE > > >> exists("SmoothData$span") >> > FALSE > >'SmoothData$span' = 'foo' exists("SmoothData$span") # TRUE>> exists("SmoothData[[2]]") >>'SmoothData[[2]]' = 'bar' exists("SmoothData[[2]]") # TRUE the problem in your case is that you have an object named 'SmoothData' with a nested component named 'span', but you're testing for the existence of an object named 'SmoothData$span'. as shown in a recent post, one attempt to do your task would be exists('SmoothData') && 'span' %in% names(SmoothData) # TRUE vQ
Duncan Murdoch
2009-May-19 16:31 UTC
[R] exists function on list objects gives always a FALSE
On 5/19/2009 12:07 PM, ?rout?k wrote:> Dear R-users, > > in a minimal example exists() gives FALSE on an object which obviously does > exist. How can I check on that list object anyway else, please? > >> SmoothData <- list(exists=TRUE, span=0.001) >> SmoothData > $exists > [1] TRUE > > $span > [1] 0.001 > >> exists("SmoothData") > TRUE > >> exists("SmoothData$span") > FALSE > >> exists("SmoothData[[2]]") > FALSE > > Thank you for any opinion regarding this topic.There is no variable with name "SmoothData$span", there is an element of SmoothData with name "span". To test for that, the safest test is probably "span" %in% names(SmoothData) but a common convention is to use is.null(SmoothData$span) because NULL elements are rare in lists. Duncan Murdoch
Romain Francois
2009-May-19 16:35 UTC
[R] exists function on list objects gives always a FALSE
?rout?k wrote:> Dear R-users, > > in a minimal example exists() gives FALSE on an object which obviously does > exist. How can I check on that list object anyway else, please? > > >> SmoothData <- list(exists=TRUE, span=0.001) >> SmoothData >> > $exists > [1] TRUE > > $span > [1] 0.001 > > >> exists("SmoothData") >> > TRUE > >> exists("SmoothData$span") >> > FALSE >This checks for existance of an object called "SmoothData$span", as in : `SmoothData$span` <- 1:10 exists("SmoothData$span") You can do: is.list( SmoothData ) && !is.null(names(SmoothData)) && "span" %in% names(SmoothData)> >> exists("SmoothData[[2]]") >> > FALSE >Similarly: `SmoothData[[2]]` <- 1 exists("SmoothData[[2]]") You can do: is.list( SmoothData ) && length(SmoothData) > 1> Thank you for any opinion regarding this topic. > Zroutik-- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
Stavros Macrakis
2009-May-19 19:26 UTC
[R] exists function on list objects gives always a FALSE
On Tue, May 19, 2009 at 12:07 PM, Žroutík <zroutik@gmail.com> wrote:> > SmoothData <- list(exists=TRUE, span=0.001) > > exists("SmoothData$span") > FALSE >As others have said, this just checks for the existence of a variable with the (strange) name "SmoothData$span". In some sense, in R semantics, xxx$yyy *always* exists if xxx is a list (or other recursive object): > xxx <- list() > xxx$hello NULL You might think that you can check names(xxx) to see if the slot has been explicitly set, but it depends on *how* you have explicitly set the slot to NULL: > xxx$hello <- 3 > xxx$hello <- NULL > names(xxx) character(0) # no names -- assigning to NULL kills slot > xxx <- list(hello=NULL) > names(xxx) [1] "hello" # 1 name -- constructing with NULL-valued slot Welcome to R! -s [[alternative HTML version deleted]]