|
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 Computer System Analyst School of Medicine Department of Biostatistics Vanderbilt University ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
Charles Dupont wrote:
> 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' > Always use is.null() to test on NULL, as in: sapply(a, is.null) Uwe Ligges > 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
Uwe Ligges <[hidden email]> writes:
> Charles Dupont wrote: > >> 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' >> > > > Always use is.null() to test on NULL, as in: What should I do if I want to check for the string "NULL"? > a <- list(NULL, "NULL", NA, "NA") > a == "NULL" [1] TRUE TRUE FALSE FALSE > a == "NA" [1] FALSE FALSE TRUE TRUE These are because of as.character: > as.character(a) [1] "NULL" "NULL" "NA" "NA" Yet, > as.character(NULL) character(0) > as.character(NA) [1] NA + seth ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
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" <[hidden email]> To: <[hidden email]> Sent: Tuesday, March 07, 2006 4:37 PM Subject: Re: [Rd] possible bug: NULL equality in lists. > Uwe Ligges <[hidden email]> writes: > >> Charles Dupont wrote: >> >>> 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' >>> >> >> >> Always use is.null() to test on NULL, as in: > > What should I do if I want to check for the string "NULL"? > >> a <- list(NULL, "NULL", NA, "NA") > >> a == "NULL" > [1] TRUE TRUE FALSE FALSE > >> a == "NA" > [1] FALSE FALSE TRUE TRUE > > These are because of as.character: > >> as.character(a) > [1] "NULL" "NULL" "NA" "NA" > > Yet, > >> as.character(NULL) > character(0) >> as.character(NA) > [1] NA > > > > + seth > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
In reply to this post by Seth Falcon-2
Seth Falcon wrote:
> Uwe Ligges <[hidden email]> writes: > > >>Charles Dupont wrote: >> >> >>>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' >>> >> >> >>Always use is.null() to test on NULL, as in: > > > What should I do if I want to check for the string "NULL"? 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 > >>a <- list(NULL, "NULL", NA, "NA") > > >>a == "NULL" > > [1] TRUE TRUE FALSE FALSE > > >>a == "NA" > > [1] FALSE FALSE TRUE TRUE > > These are because of as.character: > > >>as.character(a) > > [1] "NULL" "NULL" "NA" "NA" > > Yet, > > >>as.character(NULL) > > character(0) > >>as.character(NA) > > [1] NA > > > + seth > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
Uwe Ligges <[hidden email]> writes:
> 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") Point taken, but is the behavior of as.character correct? as.character(list(NULL)) as.character(NULL) + seth ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
In reply to this post by Uwe Ligges
Uwe Ligges wrote:
> Seth Falcon wrote: > > >>Uwe Ligges <[hidden email]> writes: >> >> >> >>>Charles Dupont wrote: >>> >>> >>> >>>>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' >>>> >>> >>> >>>Always use is.null() to test on NULL, as in: >> >> >>What should I do if I want to check for the string "NULL"? > > > 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 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 > > >>>a <- list(NULL, "NULL", NA, "NA") >> >> >>>a == "NULL" >> >>[1] TRUE TRUE FALSE FALSE >> >> >> >>>a == "NA" >> >>[1] FALSE FALSE TRUE TRUE >> >>These are because of as.character: >> >> >> >>>as.character(a) >> >>[1] "NULL" "NULL" "NA" "NA" >> >>Yet, >> >> >> >>>as.character(NULL) >> >>character(0) >> >> >>>as.character(NA) >> >>[1] NA >> >> >>+ seth >> >>______________________________________________ >>[hidden email] mailing list >>https://stat.ethz.ch/mailman/listinfo/r-devel > > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > -- Charles Dupont Computer System Analyst School of Medicine Department of Biostatistics Vanderbilt University ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
In reply to this post by Seth Falcon-2
Seth Falcon wrote:
> Uwe Ligges <[hidden email]> writes: > >>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") > > > 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. 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 > + seth > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
Uwe Ligges <[hidden email]> 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" > 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
|
Seth Falcon wrote:
> Uwe Ligges <[hidden email]> 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. > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel |
| Powered by Nabble | Edit this page |
