[R] Replace NAs in one column with data from another column
Joshua Wiley
jwiley.psych at gmail.com
Wed Sep 8 20:24:22 CEST 2010
Hi Jakob,
You can use is.na() to create an index of which rows in column 3 are
missing data, and then select these from column 1. Here is a simple
example:
dat <- data.frame(V1 = 1:5, V3 = c(1, NA, 3, 4, NA))
dat$new <- dat$V3
my.na <- is.na(dat$V3)
dat$new[my.na] <- dat$V1[my.na]
dat
This should be quite fast. I broke the steps up to be explicit, but
you can readily simplify them.
HTH,
Josh
On Wed, Sep 8, 2010 at 11:17 AM, Jakob Hedegaard
<Jakob.Hedegaard at agrsci.dk> wrote:
> Hi list,
>
> I have a data frame (m) with 169221 rows and 10 columns and would like to make a new column containing the content of column 3 but replace the NAs in column 3 with the data in column 1 (from the same row as the NA in column 3). Column 1 has data in all rows.
>
> My first attempt was:
>
> for (i in 1:169221){
> if (is.na(m[i,3])==TRUE){
> m[i,11] <- as.character(m[i,1])}
> else{
> m[i,11] <- as.character(m[i,3])}
> }
>
> Works - but takes too long time.
> I would appreciate alternative solutions.
>
> Best regards, Jakob
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list