我想将一组的值附加在一起,这里是示例:
data<-data.table(group=c("g1","g1","g2","g2","g1"),value=c(1,2,3,4,5))
我想要的输出是:
group value
1: g1 125
2: g2 34
值可以视为数字或字符。
答案 0 :(得分:1)
除了来自thelatemail的data.table
解决方案,您还可以在基础R中执行此操作:
aggregate(value ~ group, data, FUN = function(x) paste(x, collapse = ""))
或dplyr:
data %>%
group_by(group) %>%
summarise(value = paste(value, collapse = ""))