From gregory_r_warnes at groton.pfizer.com  Thu May 22 23:32:13 2003
From: gregory_r_warnes at groton.pfizer.com (gregory_r_warnes@groton.pfizer.com)
Date: Thu May 22 22:33:10 2003
Subject: [Rd] grep, gsub, sub have problems with NA values (PR#3078)
Message-ID: <200305222032.h4MKWD2b017261@pubhealth.ku.dk>


In a string context, grep, gsub, sub are improperly treating NA (missing) as
the string "NA", and returning unexpected results

> grep("A", c(NA,"NA"))
[1] 1 2

# expected: 
# [1] 2

> gsub("A", "X", c(NA,"NA"))
[1] "NX" "NX"
# expected
# [1] NA "NX"

> sub("A", "X", c(NA,"NA"))
[1] "NX" "NX"
# expected
# [1] NA "NX"

These same functions also don't like 'bare' NA's, presumably because a bare
NA is technically a factor object.  

> grep("A", NA)
Error in grep(pattern, x, ignore.case, extended, value) : 
 invalid argument

This is, understandable to users who are aware of the actual class of NA,
but it would be helpful if bare NAs were treated the same as character NAs
(when handling of these is fixed, of course!).

-Greg
 


LEGAL NOTICE\ Unless expressly stated otherwise, this message is... {{dropped}}


[Rd] grep, gsub, sub have problems with NA values (PR#3078)
gregory_r_warnes@groton.pfizer.com