Skip to content

possible bug: NULL equality in lists.

10 messages · Charles Dupont, Seth Falcon, Dimitris Rizopoulos +1 more

#
I was messing around with R and I found an example R behaving oddly:

a <- alist(NULL, "bob", c(3,6,2,3))
a
a == 'NULL'
a == "NULL"
a == 'cat'


If I create a list with a NULL value
  >a <- alist(NULL, "bob", c(3,6,2,3))
  >a
[[1]]
NULL

[[2]]
[1] "bob"

[[3]]
c(3, 6, 2, 3)

and run some tests on 'a', the '== "NULL' test returns TRUE for the NULL 
entry in the list 'a'.
  >a == 'NULL'
[1]  TRUE FALSE FALSE
  >a == "NULL"
[1]  TRUE FALSE FALSE
  >a == 'cat'
[1]  FALSE FALSE FALSE

This is consistent for every example of NULL's in a list that I can 
think of.

Is this a bug or undocumented correct behavior?

Here is my version output

platform i486-pc-linux-gnu
arch     i486
os       linux-gnu
system   i486, linux-gnu
status
major    2
minor    2.0
year     2005
month    10
day      06
svn rev  35749
language R


Thanks

Charles
#
Charles Dupont wrote:

            
Always use is.null() to test on NULL, as in:

   sapply(a, is.null)

Uwe Ligges
#
Uwe Ligges <ligges at statistik.uni-dortmund.de> writes:
What should I do if I want to check for the string "NULL"?
[1]  TRUE  TRUE FALSE FALSE
[1] FALSE FALSE  TRUE  TRUE

These are because of as.character:
[1] "NULL" "NULL" "NA"   "NA"  

Yet,
character(0)
[1] NA



+ seth
#
I think it'd be more appropriate to use:

sapply(a, "==", "NULL")

or

sapply(a, "==", "NA")

for this case.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Seth Falcon" <sfalcon at fhcrc.org>
To: <r-devel at stat.math.ethz.ch>
Sent: Tuesday, March 07, 2006 4:37 PM
Subject: Re: [Rd] possible bug: NULL equality in lists.
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
Seth Falcon wrote:

            
These are all dangerous, hence use the "safe" ways:

sapply(a, is.null)
sapply(a, identical, "NULL")
sapply(a, is.na)
sapply(a, identical, "NA")

Best,
Uwe
#
Uwe Ligges <ligges at statistik.uni-dortmund.de> writes:
Point taken, but is the behavior of as.character correct?

as.character(list(NULL))

as.character(NULL)

+ seth
#
Uwe Ligges wrote:
For the NA list problem.  It would be better if 'as.character' when
converting a list when it finds a NA value to set that element to the
string NA value not "NA" the string.


Charles

  
    
#
Seth Falcon wrote:

            
I thought about it quite a while. I think the current bahaviour is quite 
OK. What should as.character do in the follwing case:
   as.character(list("A", NA, NULL))
?

Note that it only can return a character vector...!

So, should it return a character vector of length 2? That's a bad idea, 
if the length is reduced.

Moreover, as.character() does not get a NA or a NULL object for coercion 
but an element of type list that itself conatins NA or NULL...

So if you want to convert a list to character and *keep* NA/NULL values, 
you can only say:

  lapply(a, as.character)

Uwe
#
Uwe Ligges <ligges at statistik.uni-dortmund.de> writes:
But consistent with vectorizing a list using unlist:

unlist(list(NULL, NULL, "a"))
[1] "a"
In the case of NA, I think converting to "NA" should be a last
resort.  Since NA is a perfectly valid element of a character vector,
it would seem to be a better choice.
#
Seth Falcon wrote:

            
Seth, as.character() does NOT unlist anything, it converts the *list* 
objects to character, the one element case might be a to trivial 
example, instead consider:


 > as.character(list(c("a", "b"), NULL, c(NA, NULL, 2)))
[1] "c(\"a\", \"b\")" "NULL"            "c(NA, 2)"

I think you simply should not apply as.character on a list as a whole in 
order to get what you are expecting ...

Best,
Uwe