[Rd] possible bug: NULL equality in lists.

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Mar 7 19:37:02 CET 2006


Seth Falcon wrote:

> 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)
>>
>>
>>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.
> 
> 
> But consistent with vectorizing a list using unlist:
> 
> unlist(list(NULL, NULL, "a"))
> [1] "a"


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




>>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...
> 
> 
> 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.



> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list