Replace NAs in one variable with values from another variable

前端 未结 4 1155
终归单人心
终归单人心 2020-12-10 18:25

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:

<         


        
4条回答
  •  情深已故
    2020-12-10 19:03

    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))
    

提交回复
热议问题