[R] ifelse and wildcard matching
Duncan Murdoch
murdoch at stats.uwo.ca
Sun Dec 6 21:10:38 CET 2009
On 06/12/2009 3:05 PM, Jennifer Brea wrote:
> Hi,
>
> I'm trying to create a new variable using the following code:
>
> A$B2<-ifelse(A$B %in% "EDUCATION","EDUCATION",NA)
>
> The problem is, I want the new variable to be labeled "EDUCATION" if the
> original *contains* "EDUCATION", not of it is an exact match. I've done
> a lot of searching on wild cards and the like, and the only function
> I've found that does this kind of matching is grep(), which I don't
> think I can use here, since it subsets the original data.
grepl() is probably easiest:
A$B2 <- A$B
A$B2[ !grepl("EDUCATION", A$B2) ] <- NA
Duncan Murdoch
More information about the R-help
mailing list