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:
<
One easy way is to use pmax with na.rm = TRUE:
pmax
na.rm = TRUE
pmax(VAR1, VAR2, na.rm = TRUE)
The command for data frame dat:
dat
dat <- transform(dat, VAR3 = pmax(VAR1, VAR2, na.rm = TRUE))