Merge across two columns with dplyr

前端 未结 1 1453
闹比i
闹比i 2021-01-28 15:03

I am looking to merge across two columns which contain different data. That is where it is empty in one column it is not empty in another, essentially data collected from two di

相关标签:
1条回答
  • 2021-01-28 15:34

    You can use dplyr::coalesce function which selects values from columns that first appear as non NA; say if you want to combine the first two columns:

    check %>% mutate(cat = coalesce(t7_1_ExpA_Intro1, t7_1_ExpB_Intro1))
    # or check %>% mutate(cat = do.call(coalesce, .[1:n])) if you have more columns to coalesce
    
    # A tibble: 6 x 5
    #  t7_1_ExpA_Intro1 t7_1_ExpB_Intro1 t7_1_ExpA_DV t7_1_ExpB_DV   cat
    #             <int>            <int>        <int>        <int> <int>
    #1               NA               NA           NA           NA    NA
    #2               NA               NA           NA           NA    NA
    #3               NA               NA           NA           NA    NA
    #4               NA                3           NA            3     3
    #5               NA               NA           NA           NA    NA
    #6               NA               NA           NA           NA    NA
    
    0 讨论(0)
提交回复
热议问题