as.character(list(NA))

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

as.character(list(NA))

Patrick Perry-2
As of R Under development (unstable) (2018-01-19 r74138):

 > as.character(list(NA))
[1] "NA"

 > is.na(as.character(list(NA)))
[1] FALSE

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.character(list(NA))

Dirk Eddelbuettel

On 20 January 2018 at 10:43, Patrick Perry wrote:
| As of R Under development (unstable) (2018-01-19 r74138):
|
|  > as.character(list(NA))
| [1] "NA"
|
|  > is.na(as.character(list(NA)))
| [1] FALSE

Are you aware that this is the same in R-release, and might be "on purpose" ?

R> R.Version()$version.string
[1] "R version 3.4.3 (2017-11-30)"
R> as.character(list(NA))
[1] "NA"
R> is.na(as.character(list(NA)))
[1] FALSE
R>

It is after all you who forces the as.character() and I don't think anybody
claims that to be a lossless operation from logical.  To wit:

R> is.na(list(NA))
[1] TRUE
R>

Dirk

--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.character(list(NA))

R devel mailing list
In reply to this post by Patrick Perry-2
I believe that for a list as.character() applies deparse()  to each element
of the list.  deparse() does not preserve NA-ness, as it is intended to
make text that the parser can read.

> str(as.character(list(Na=NA, LglVec=c(TRUE,NA),
Function=function(x){x+1})))
 chr [1:3] "NA" "c(TRUE, NA)" "function (x) \n{\n    x + 1\n}"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Jan 20, 2018 at 7:43 AM, Patrick Perry <[hidden email]> wrote:

> As of R Under development (unstable) (2018-01-19 r74138):
>
> > as.character(list(NA))
> [1] "NA"
>
> > is.na(as.character(list(NA)))
> [1] FALSE
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.character(list(NA))

Hervé Pagès-2
On 01/20/2018 08:24 AM, William Dunlap via R-devel wrote:
> I believe that for a list as.character() applies deparse()  to each element
> of the list.  deparse() does not preserve NA-ness, as it is intended to
> make text that the parser can read.
>
>> str(as.character(list(Na=NA, LglVec=c(TRUE,NA),
> Function=function(x){x+1})))
>   chr [1:3] "NA" "c(TRUE, NA)" "function (x) \n{\n    x + 1\n}"
>

This really comes as a surprise though since coercion to all the
other atomic types (except raw) preserve the NAs.

And also as.character(unlist(list(NA))) preserves them.

H.

>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Sat, Jan 20, 2018 at 7:43 AM, Patrick Perry <[hidden email]> wrote:
>
>> As of R Under development (unstable) (2018-01-19 r74138):
>>
>>> as.character(list(NA))
>> [1] "NA"
>>
>>> is.na(as.character(list(NA)))
>> [1] FALSE
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
>

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.character(list(NA))

Robert McGehee-2
Also perhaps a surprise that the behavior depends on the mode of the NA.

> is.na(as.character(list(NA_real_)))
[1] FALSE
> is.na(as.character(list(NA_character_)))
[1] TRUE

Does this mean deparse() preserves NA-ness for NA_character_ but not NA_real_?


-----Original Message-----
From: R-devel [mailto:[hidden email]] On Behalf Of Hervé Pagès
Sent: Monday, January 22, 2018 2:01 PM
To: William Dunlap <[hidden email]>; Patrick Perry <[hidden email]>
Cc: [hidden email]
Subject: Re: [Rd] as.character(list(NA))

On 01/20/2018 08:24 AM, William Dunlap via R-devel wrote:
> I believe that for a list as.character() applies deparse()  to each element
> of the list.  deparse() does not preserve NA-ness, as it is intended to
> make text that the parser can read.
>
>> str(as.character(list(Na=NA, LglVec=c(TRUE,NA),
> Function=function(x){x+1})))
>   chr [1:3] "NA" "c(TRUE, NA)" "function (x) \n{\n    x + 1\n}"
>

This really comes as a surprise though since coercion to all the
other atomic types (except raw) preserve the NAs.

And also as.character(unlist(list(NA))) preserves them.

H.

>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Sat, Jan 20, 2018 at 7:43 AM, Patrick Perry <[hidden email]> wrote:
>
>> As of R Under development (unstable) (2018-01-19 r74138):
>>
>>> as.character(list(NA))
>> [1] "NA"
>>
>>> is.na(as.character(list(NA)))
>> [1] FALSE
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
>

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.character(list(NA))

R devel mailing list
I tend to avoid using as.<type> functions on lists, since they act oddly in
several ways.
E.g, if the list "L" consists entirely of scalar elements then
as.numeric(L) acts like
as.numeric(unlist(L)) but if any element is not a scalar there is an
error.  as.character()
does not seem to make a distinction between the all-scalar and
not-all-scalar cases
but does various things with NA's of various types.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Jan 22, 2018 at 11:14 AM, Robert McGehee <
[hidden email]> wrote:

> Also perhaps a surprise that the behavior depends on the mode of the NA.
>
> > is.na(as.character(list(NA_real_)))
> [1] FALSE
> > is.na(as.character(list(NA_character_)))
> [1] TRUE
>
> Does this mean deparse() preserves NA-ness for NA_character_ but not
> NA_real_?
>
>
> -----Original Message-----
> From: R-devel [mailto:[hidden email]] On Behalf Of Hervé
> Pagès
> Sent: Monday, January 22, 2018 2:01 PM
> To: William Dunlap <[hidden email]>; Patrick Perry <
> [hidden email]>
> Cc: [hidden email]
> Subject: Re: [Rd] as.character(list(NA))
>
> On 01/20/2018 08:24 AM, William Dunlap via R-devel wrote:
> > I believe that for a list as.character() applies deparse()  to each
> element
> > of the list.  deparse() does not preserve NA-ness, as it is intended to
> > make text that the parser can read.
> >
> >> str(as.character(list(Na=NA, LglVec=c(TRUE,NA),
> > Function=function(x){x+1})))
> >   chr [1:3] "NA" "c(TRUE, NA)" "function (x) \n{\n    x + 1\n}"
> >
>
> This really comes as a surprise though since coercion to all the
> other atomic types (except raw) preserve the NAs.
>
> And also as.character(unlist(list(NA))) preserves them.
>
> H.
>
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Sat, Jan 20, 2018 at 7:43 AM, Patrick Perry <[hidden email]>
> wrote:
> >
> >> As of R Under development (unstable) (2018-01-19 r74138):
> >>
> >>> as.character(list(NA))
> >> [1] "NA"
> >>
> >>> is.na(as.character(list(NA)))
> >> [1] FALSE
> >>
> >> ______________________________________________
> >> [hidden email] mailing list
> >> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=
> BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
> VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=
> Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.
> ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=
> BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=
> VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=
> Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
> >
>
> --
> Hervé Pagès
>
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M1-B514
> P.O. Box 19024
> Seattle, WA 98109-1024
>
> E-mail: [hidden email]
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: as.character(list(NA))

Hervé Pagès-2
On 01/22/2018 01:02 PM, William Dunlap wrote:
> I tend to avoid using as.<type> functions on lists, since they act oddly
> in several ways.
> E.g, if the list "L" consists entirely of scalar elements then
> as.numeric(L) acts like
> as.numeric(unlist(L)) but if any element is not a scalar there is an
> error.

FWIW personally I see this as a nice feature and use as.numeric(L)
instead of as.numeric(unlist(L) in places where I'd rather fail than
getting something that is not parallel to the input.

H.

>  as.character()
> does not seem to make a distinction between the all-scalar and
> not-all-scalar cases
> but does various things with NA's of various types.
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__tibco.com&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=TBOnlxYy_MjXJqxLqI8WB2WWHNMtc8qw5gxsqUoyoH4&s=XiTysBsoZb4M91NAS4DK6nK982wAf7JGpRSDrXioQ3A&e=>
>
> On Mon, Jan 22, 2018 at 11:14 AM, Robert McGehee
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Also perhaps a surprise that the behavior depends on the mode of the NA.
>
>      > is.na
>     <https://urldefense.proofpoint.com/v2/url?u=http-3A__is.na&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=TBOnlxYy_MjXJqxLqI8WB2WWHNMtc8qw5gxsqUoyoH4&s=z8HTNapemGDhhH0ICUN2hgJrtUcxsgM96mcUwD8QzQk&e=>(as.character(list(NA_real_)))
>     [1] FALSE
>      > is.na
>     <https://urldefense.proofpoint.com/v2/url?u=http-3A__is.na&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=TBOnlxYy_MjXJqxLqI8WB2WWHNMtc8qw5gxsqUoyoH4&s=z8HTNapemGDhhH0ICUN2hgJrtUcxsgM96mcUwD8QzQk&e=>(as.character(list(NA_character_)))
>     [1] TRUE
>
>     Does this mean deparse() preserves NA-ness for NA_character_ but not
>     NA_real_?
>
>
>     -----Original Message-----
>     From: R-devel [mailto:[hidden email]
>     <mailto:[hidden email]>] On Behalf Of Hervé Pagès
>     Sent: Monday, January 22, 2018 2:01 PM
>     To: William Dunlap <[hidden email] <mailto:[hidden email]>>;
>     Patrick Perry <[hidden email] <mailto:[hidden email]>>
>     Cc: [hidden email] <mailto:[hidden email]>
>     Subject: Re: [Rd] as.character(list(NA))
>
>     On 01/20/2018 08:24 AM, William Dunlap via R-devel wrote:
>      > I believe that for a list as.character() applies deparse()  to
>     each element
>      > of the list.  deparse() does not preserve NA-ness, as it is
>     intended to
>      > make text that the parser can read.
>      >
>      >> str(as.character(list(Na=NA, LglVec=c(TRUE,NA),
>      > Function=function(x){x+1})))
>      >   chr [1:3] "NA" "c(TRUE, NA)" "function (x) \n{\n    x + 1\n}"
>      >
>
>     This really comes as a surprise though since coercion to all the
>     other atomic types (except raw) preserve the NAs.
>
>     And also as.character(unlist(list(NA))) preserves them.
>
>     H.
>
>      >
>      > Bill Dunlap
>      > TIBCO Software
>      > wdunlap tibco.com
>     <https://urldefense.proofpoint.com/v2/url?u=http-3A__tibco.com&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=TBOnlxYy_MjXJqxLqI8WB2WWHNMtc8qw5gxsqUoyoH4&s=XiTysBsoZb4M91NAS4DK6nK982wAf7JGpRSDrXioQ3A&e=>
>      >
>      > On Sat, Jan 20, 2018 at 7:43 AM, Patrick Perry
>     <[hidden email] <mailto:[hidden email]>> wrote:
>      >
>      >> As of R Under development (unstable) (2018-01-19 r74138):
>      >>
>      >>> as.character(list(NA))
>      >> [1] "NA"
>      >>
>      >>> is.na
>     <https://urldefense.proofpoint.com/v2/url?u=http-3A__is.na&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=TBOnlxYy_MjXJqxLqI8WB2WWHNMtc8qw5gxsqUoyoH4&s=z8HTNapemGDhhH0ICUN2hgJrtUcxsgM96mcUwD8QzQk&e=>(as.character(list(NA)))
>      >> [1] FALSE
>      >>
>      >> ______________________________________________
>      >> [hidden email] <mailto:[hidden email]> mailing list
>      >>
>     https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
>     <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=>
>      >>
>      >
>      >       [[alternative HTML version deleted]]
>      >
>      > ______________________________________________
>      > [hidden email] <mailto:[hidden email]> mailing list
>      >
>     https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=
>     <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwICAg&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=VbamM9XRQOlfBakrmlrmQZ7DLgXZ-hhhFeLD-fKpoCo&s=Luhqwpr2bTltIA9Cy7kA4gwcQh16bla0S6OVe3Z09Xo&e=>
>      >
>
>     --
>     Hervé Pagès
>
>     Program in Computational Biology
>     Division of Public Health Sciences
>     Fred Hutchinson Cancer Research Center
>     1100 Fairview Ave. N, M1-B514
>     P.O. Box 19024
>     Seattle, WA 98109-1024
>
>     E-mail: [hidden email] <mailto:[hidden email]>
>     Phone: (206) 667-5791 <tel:%28206%29%20667-5791>
>     Fax: (206) 667-1319 <tel:%28206%29%20667-1319>
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel
>     <https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwMFaQ&c=eRAMFD45gAfqt84VtBcfhQ&r=BK7q3XeAvimeWdGbWY_wJYbW0WYiZvSXAJJKaaPhzWA&m=TBOnlxYy_MjXJqxLqI8WB2WWHNMtc8qw5gxsqUoyoH4&s=Tl76BG1PB4_MNRObhik4eJ3UG8DsFBcgCq_k4f7RBSE&e=>
>
>

--
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: [hidden email]
Phone:  (206) 667-5791
Fax:    (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel