An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110902/5af6da3e/attachment.pl>
conditional replacement of character strings in vectors
6 messages · David Winsemius, R. Michael Weylandt, Josh Tewksbury
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110902/edeb55ac/attachment.pl>
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
On Sep 2, 2011, at 7:51 PM, R. Michael Weylandt wrote:
Your data frame didn't come across legibly, try sending it in plain text using the dput() command. That said, I'd guess you want something like this: d[is.na(d$a),"a"] <- d[is.na(d$b),"b"]
One of the rare instances where I disagree with Michael. The row index on the right hand side must be the same as the row index on the left hand side.
The idea is that is.na(d$a) selects only those rows where column "a" is NA and then moves b values into a for only those rows.
Right, that is the idea.
Write back with the dput() data frame if this doesn't work. Hope this helps, Michael Weylandt On Fri, Sep 2, 2011 at 3:51 PM, Josh Tewksbury <tewksjj at uw.edu> 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. Thanks -- Josh
David Winsemius, MD West Hartford, CT
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110902/3f552ed5/attachment.pl>
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110904/ef9d7e8f/attachment.ksh>