s-news
[Top] [All Lists]

Re: NA assignment for character vector

To: "Gezmu, Misrak (NIH/NIAID)" <MGEZMU@niaid.nih.gov>
Subject: Re: NA assignment for character vector
From: ripley@stats.ox.ac.uk
Date: Wed, 26 Feb 2003 14:45:47 +0000 (GMT)
Cc: "'Sundar Dorai-Raj'" <sundar.dorai-raj@pdf.com>, Stephen Lake <slake@hsph.harvard.edu>, Splus Users Group <s-news@lists.biostat.wustl.edu>
In-reply-to: <C07A4927D67EFB4DB482371766C53415DCDEBC@kinetic.niaid.nih.gov>
On Tue, 25 Feb 2003, Gezmu, Misrak (NIH/NIAID) wrote:

> The is.na function is not doing its job. Until S-Plus fixes the problem,

What makes you say that?  S-PLUS does not have missing elements 
in character vectors.  "NA" is not missing, and is a perfectly good
abbreviation (for Nabisco and North America as I recall).  I believe is.na 
is working exactly as it is intended to.

> here is my suggestion.  
> Copy the result of is.na to try.ok, then use "for" and "if" statements to
> assign "T" to the positions with "NA". try.ok has the results needed. 
> 
> try<-c("a","b","c","NA","e")
> > is.na(try)
> [1] F F F F F
> > try.ok<-is.na(try)
> > for (i in 1:5) {if (try[i]=="NA") try.ok[i]<-T}
> > try
> [1] "a"  "b"  "c"  "NA" "e" 
> > try.ok
> [1] F F F T F

try == "NA" does _that_ much more simply, but it does not make the values
missing.

R does have missing elements in character vectors, so there try[i] == "NA"
would be missing for a missing element.  That might be what you want, but 
then try[i] == "a" will also be missing, so a _lot_ more care is needed.


> -----Original Message-----
> From: Sundar Dorai-Raj [mailto:sundar.dorai-raj@pdf.com]
> Sent: Tuesday, February 25, 2003 11:55 AM
> To: Stephen Lake
> Cc: Splus Users Group
> Subject: Re: [S] NA assignment for character vector
> 
> 
> 
> 
> Stephen Lake wrote:
> > Hi S-users,
> > 
> > Could someone please tell me how I can assign NA's to
> > character vectors and matrices? The usual element
> > assignment does not seem to work. Here is an example:
> > 
> > 
> >>vec <- letters[1:5]
> >>vec
> > 
> > [1] "a" "b" "c" "d" "e"
> > 
> >>vec[4] <- NA
> >>vec
> > 
> > [1] "a" "b" "c" "NA" "e"
> > 
> >>is.na(vec)
> > 
> > [1] F F F F F
> > 
> > If instead of letters[1:5] I assign the numeric vector
> > 1:5, the behavior is as expected, ie the is.na() function
> > detects a NA at position 4 of the vector.
> > 
> > Any help would be greatly appreciated,
> > Steve Lake
> > 
> > PS I am using Version 6.0 Release 1 for Sun SPARC, SunOS 5.5
> > 
> > 
> 
>  From ?is.na:
> 
> REQUIRED ARGUMENTS:
> x     an S-PLUS object, which should be logical, numeric, or complex.
> 
> WARNING:
> This is always FALSE when x is of mode "character".
> 
> 
> You should use == "NA" if x is character. As in:
> 
>  > vec <- letters[1:5]
>  > vec[4] <- NA
>  > vec == "NA"
> [1] F F F T F
> 
> 
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
> 

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



<Prev in Thread] Current Thread [Next in Thread>