[R] conditional replacement of character strings in vectors

David Winsemius dwinsemius at comcast.net
Sat Sep 3 01:52:03 CEST 2011


On Sep 2, 2011, at 3:51 PM, Josh Tewksbury wrote:

> Hello, I have a dataframe that looks like this:
>
>  a b  NA Honduras  China NA  NA Sudan  Japan NA  NA Mexico  NA Mexico
> I would like to replace the NA values in column b with the non-NA  
> values in
> column a.  I have tried a number of techniques, (if, ifelse) but I  
> must have
> the logic wrong.

Mangled data but no matter:

dfrm$b[is.na(dfrm$b)] <- dfrm$a[is.na(dfrm$b)]

(Learn to post in plain text. This is a plain text list.)


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list