[Rd] grep, gsub, sub have problems with NA values (PR#3078)

gregory_r_warnes at groton.pfizer.com gregory_r_warnes at groton.pfizer.com
Thu May 22 23:32:13 MEST 2003


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}}



More information about the R-devel mailing list