5

How can I replace the "NAs" of a variable VAR1 with with the values of the second variable VAR2 to create a third variable VAR3 in R? The data looks like this:

 VAR1:    VAR2:
 1        NA
 3        NA
 NA       1 
 NA       3
 2        NA 
 NA       1

Afterwards it should look likes this:

 VAR1:    VAR2:    VAR3:
 1        NA       1   
 3        NA       3
 NA       1        1
 NA       3        3
 2        NA       2
 NA       1        1  
Thomas
  • 40,508
  • 11
  • 98
  • 131
maller
  • 227
  • 1
  • 4
  • 12

4 Answers4

7

One way is to use ifelse:

DF <- transform(DF, VAR3 = ifelse(!is.na(VAR1), VAR1, VAR2))

where transform was used to avoid typing DF$ over and over, but maybe you will prefer:

DF$VAR3 <- ifelse(!is.na(DF$VAR1), DF$VAR1, DF$VAR2)
flodel
  • 82,429
  • 18
  • 167
  • 205
4

One easy way is to use pmax with na.rm = TRUE:

pmax(VAR1, VAR2, na.rm = TRUE)

The command for data frame dat:

dat <- transform(dat, VAR3 = pmax(VAR1, VAR2, na.rm = TRUE))
Sven Hohenstein
  • 75,536
  • 15
  • 130
  • 155
1

Here is another approach though I like the transform:

##  dat <- read.table(text="VAR1    VAR2
##   1        NA
##   3        NA
##   NA       1 
##   NA       3
##   2        NA


dat$VAR3 <- dat[, "VAR1"]
dat[is.na(dat[, "VAR3"]), "VAR3"] <- dat[is.na(dat[, "VAR3"]), "VAR2"]
dat
##   VAR1. VAR2. VAR3
## 1     1    NA    1
## 2     3    NA    3
## 3    NA     1    1
## 4    NA     3    3
## 5     2    NA    2
## 6    NA     1    1
Tyler Rinker
  • 99,090
  • 56
  • 292
  • 477
1
library(dplyr)
df %>% mutate(VAR3=coalesce(VAR1,VAR2))
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
  • 1
    it would be great if you could add some explanation to make this a high-quality answer – Azsgy Feb 02 '18 at 01:36