combine rows in data frame containing NA to make complete row

前端 未结 6 1945
不思量自难忘°
不思量自难忘° 2020-11-29 10:17

I know this is a duplicate Q but I can\'t seem to find the post again

Using the following data

df <- data.frame(A=c(1,1,2,2),B=c(NA,2,NA,4),C=c(3,         


        
6条回答
  •  眼角桃花
    2020-11-29 10:53

    I haven't figured out how to put the coalesce_by_column function inside the dplyr pipeline, but this works:

    coalesce_by_column <- function(df) {
      return(coalesce(df[1], df[2]))
    }
    
    df %>%
      group_by(A) %>%
      summarise_all(coalesce_by_column)
    
    ##       A     B     C     D     E
    ##       
    ## 1     1     2     3     2     5
    ## 2     2     4     5     3     4
    

    Edit: include @Jon Harmon's solution for more than 2 members of a group

    # Supply lists by splicing them into dots:
    coalesce_by_column <- function(df) {
      return(dplyr::coalesce(!!! as.list(df)))
    }
    
    df %>%
      group_by(A) %>%
      summarise_all(coalesce_by_column)
    
    #> # A tibble: 2 x 5
    #>       A     B     C     D     E
    #>       
    #> 1     1     2     3     2     5
    #> 2     2     4     5     3     4
    

提交回复
热议问题