[R] paste and NAs

Benjamin.STABLER@odot.state.or.us Benjamin.STABLER at odot.state.or.us
Wed Jul 23 22:00:45 CEST 2003


I didn't realize that ifelse is vectorized.  Thanks for the suggestion.

>-----Original Message-----
>From: Thomas Lumley [mailto:tlumley at u.washington.edu]
>Sent: Wednesday, July 23, 2003 11:50 AM
>To: STABLER Benjamin
>Cc: r-help at stat.math.ethz.ch; GREGOR Brian J
>Subject: Re: [R] paste and NAs
>
>
>On Wed, 23 Jul 2003 Benjamin.STABLER at odot.state.or.us wrote:
>
>> I understand how R treats NAs but in the situation below it 
>would be nice to
>> have a na.skip argument to paste so it does not convert the 
>NAs to "NA"s and
>> simply skips those elements of the vector for pasting.  
>> 
>> > x
>> [1] "2.13" "2.3"  NA     NA     "2.83" NA    
>> 
>> > paste(x, "0", sep="")
>> [1] "2.130" "2.30"  "NA0"   "NA0"   "2.830" "NA0"  
>> 
>> With na.skip argument:
>> 
>> paste(x, "0", sep="", na.skip=T)
>> [1] "2.130" "2.30"  NA   NA   "2.830" NA  
>
>How about
>   ifelse(is.na(x), NA, paste(x,"0",sep=""))
>
>> Otherwise I will use:
>> 
>> y <- paste(x, "0", sep="")
>> gsub(paste("NA","0",sep=""),"", y)
>> 
>> "" is not the same as NA, but for my purposes "" is close 
>enough.  Why can't
>> I use NA as a replacement in gsub, even though c("a", NA) works?
>> 
>
>
>You need to use a character NA. Plain NA is a logical. SO
>gsub("NA0",as.character(NA), y)
>will work.  This is arguably a bug.
>
>      -thomas
>
>
>Thomas Lumley			Assoc. Professor, Biostatistics
>tlumley at u.washington.edu	University of Washington, Seattle
>
>




More information about the R-help mailing list