How can I combine rows within the same data frame in R (based on duplicate values under a specific column)?

后端 未结 4 1333
小鲜肉
小鲜肉 2021-01-07 03:39

Sample of 2 (made-up) example rows in df:

userid   facultyid  courseid schoolid
167       265        NA       1678  
167       71111      301      NA
         


        
4条回答
  •  灰色年华
    2021-01-07 04:22

    aggregate(x = df1, by = list(df1$userid), FUN = function(x) na.omit(x)[1])[,-1]
    

    or use dplyr library:

    library(dplyr)
    
    df1 %>%
      group_by(userid) %>%
      summarise_each(funs(first(na.omit(.))))
    

提交回复
热议问题