【发布时间】:2018-12-06 17:02:31
【问题描述】:
我在下面有一个示例数据框。
eg_data <- data.frame(
time = c("1", "1", "2","2"),
type = c("long", "short","long", "short"),
size=c(200,50, 500, 150 ))
我需要为每个时间段创建总计大小值的行。我已经查看了聚合和 by 的组合,但我无法让它正常工作。
我尝试过的一个例子:
rbind(eg_data, data.frame(time="1 + 2", type="long", size=by(eg_data$size,
eg_data$time=="long", sum)))
我希望最终数据框看起来像的示例:
eg_data <- data.frame(
time = c("1", "1", "2","2", "1 + 2", "1 + 2"),
type = c("long", "short","long", "short", "long", "short"),
size=c(200, 50, 500, 150, 700, 200))
感谢任何帮助,非常感谢基于 R 的解决方案。
【问题讨论】:
标签: r group-by summarization