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

后端 未结 4 1294
小鲜肉
小鲜肉 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:17

    Here's a simple one-liner from plyr. I wrote it a bit more generally than you asked:

     a <- data.frame(x=c(1,2,3,1,2,3,1,2,3),y=c(2,3,1,1,2,3,2,3,1),
           z=c(NA,1,NA,2,NA,3,4,NA,5),zz=c(1,NA,2,NA,3,NA,4,NA,5))
    
     ddply(a,~x+y,summarize,z=first(z[!is.na(z)]),zz=first(zz[!is.na(zz)]))
    

    Specifically answering the original question, if your data frame is named a, :

     ddply(a,~userid,summarize,facultyid=first(facultyid[!is.na(facultyid)]),
             courseid=first(courseid[!is.na(courseid)],
             schoolid=first(schoolid[!is.na(schoolid)])
    

提交回复
热议问题