Replacing NAs in a column with the values of other column

前端 未结 1 994
别跟我提以往
别跟我提以往 2021-02-07 21:54

I wonder how to replace NAs in a column with the values of other column in R using dplyr. MWE is below.

Letters <- LETT         


        
相关标签:
1条回答
  • 2021-02-07 22:22

    You can use coalesce:

    library(dplyr)
    
    df1 <- data.frame(Letters, Char, stringsAsFactors = F)
    
    df1 %>%
      mutate(Char1 = coalesce(Char, Letters))
    
      Letters Char Char1
    1       A    a     a
    2       B    b     b
    3       C <NA>     C
    4       D    d     d
    5       E <NA>     E
    
    0 讨论(0)
提交回复
热议问题