[R] exists function on list objects gives always a FALSE

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue May 19 18:30:35 CEST 2009


Ž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




More information about the R-help mailing list