Group by ID, each element of the new table is a vector

前端 未结 3 1876
醉酒成梦
醉酒成梦 2020-12-21 16:09

I have a table like this

data.table(ID = c(1,2,3,4,5,6), 
         R = c(\"s\",\"s\",\"n\",\"n\",\"s\",\"s\"), 
         S = c(\"a\",\"a\",\"a\",\"b\",\"b\",         


        
3条回答
  •  心在旅途
    2020-12-21 16:38

    You can use dcast from reshape2 with the appropriate aggregating function:

    library(functional)
    library(reshape2)
    
    dcast(df, R~S, value.var='ID', fun.aggregate=Curry(paste0, collapse=','))
    #  R   a   b
    #1 n   3   4
    #2 s 1,2 5,6
    

    Or even short as @akrun underlined:

    dcast(df, R~S, value.var='ID', toString)
    

提交回复
热议问题