[R] issue with nzchar() ?

David L Carlson dcarlson at tamu.edu
Mon Aug 6 19:19:37 CEST 2012


It would be nice to be able to trigger NA returning NA with an argument to
the function, but you can easily get that result:

> ifelse(is.na(x), NA, nzchar(x))
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[25]  TRUE  TRUE    NA FALSE

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Bert Gunter
> Sent: Monday, August 06, 2012 10:43 AM
> To: Liviu Andronic
> Cc: r-help at r-project.org Help
> Subject: Re: [R] issue with nzchar() ?
> 
> Liviu:
> 
> Well, as usual, to a certain extent this is arbitrary and the only
> issue is whether it is documented correctly.
> 
> To me, NA (of whatever mode) means ""indeterminate" or "unknown," so
> since "" is known and of length 0, I would have expected NA as a
> return. But the point is, not what our particular tastes are ("You say
> 'tomayto', I say 'tomahto,' an old song goes), but what the docs say.
> And in both cases, they tell you exactly what you'll get.
> 
> For nchar(): " an integer vector giving the sizes of each element,
> currently always 2 for missing values (for NA)"
> 
> and for nzchar: "a logical vector of the same length as x, true if and
> only if the element has non-zero length." (note the 'only if').
> 
> So I see no error or inconsistencies anywhere.
> 
> -- Bert
> 
> On Mon, Aug 6, 2012 at 7:53 AM, Liviu Andronic <landronimirc at gmail.com>
> wrote:
> > On Mon, Aug 6, 2012 at 4:48 PM, Liviu Andronic
> <landronimirc at gmail.com> wrote:
> >> string, something that I find strange. At best NA is the equivalent
> of
> >> an empty string. In this sense, if you Hmisc::describe() the vector
> >> you get, as I would expect, that in the context of character vectors
> >> NA and '' values are considered together:
> >>
> >
> > By the way, same question holds for nchar(): Should NA values be
> > reported as 2-char strings, or as 0-char empty/missing values?
> >> x <- c(letters, NA, '')
> >> nchar(x)
> >  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 0
> >
> >
> > Liviu
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
> --
> 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-
> biostatistics/pdb-ncb-home.htm
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list