s-news
[Top] [All Lists]

Re: NA assignment for character vector

To: Sundar Dorai-Raj <sundar.dorai-raj@PDF.COM>
Subject: Re: NA assignment for character vector
From: Spencer Graves <spencer.graves@PDF.COM>
Date: Tue, 25 Feb 2003 09:18:23 -0800
Cc: Stephen Lake <slake@hsph.harvard.edu>, Splus Users Group <s-news@lists.biostat.wustl.edu>
References: <Pine.GSO.4.53.0302251140360.5708@hsph.harvard.edu> <3E5B9FE0.4010507@pdf.com>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01
That's one of the difference between S-Plus and R. In R 1.6.2, I just got the following:

> vec <- letters[1:5]
> vec[4] <- NA
> is.na(vec)
[1] FALSE FALSE FALSE  TRUE FALSE
> vec == "NA"
[1] FALSE FALSE FALSE    NA FALSE

People wishing to write code that works in both S-Plus and R need to be aware of this.
Spencer Graves

Sundar Dorai-Raj wrote:


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



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