将行添加到数据框中,并包含组内数据的总和

Ada*_*m_S 2 group-by r summarization

我下面有一个示例数据框。

eg_data <- data.frame(
time = c("1", "1", "2","2"), 
type = c("long", "short","long", "short"), 
size=c(200,50, 500, 150 ))
Run Code Online (Sandbox Code Playgroud)

我需要创建行来汇总每个时间段的大小值。我已经查看了aggregate 和by 的组合,但我无法让它正常工作。

我尝试过的一个例子:

rbind(eg_data, data.frame(time="1 + 2", type="long", size=by(eg_data$size, 
eg_data$time=="long", sum)))   
Run Code Online (Sandbox Code Playgroud)

我希望最终数据框的示例如下:

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))
Run Code Online (Sandbox Code Playgroud)

任何帮助都是值得赞赏的,带有基本 R 的解决方案将非常感激。

Ant*_*osK 5

eg_data <- data.frame(
  time = c("1", "1", "2","2"), 
  type = c("long", "short","long", "short"), 
  size=c(200,50, 500, 150 ))

library(dplyr)

eg_data %>%
  group_by(type) %>%                               # for each type
  summarise(time = paste(time, collapse = " + "),  # combine times
            size = sum(size)) %>%                  # get sum of sizes
  bind_rows(eg_data, .)                            # add everything after your original dataset (rows)

#    time  type size
# 1     1  long  200
# 2     1 short   50
# 3     2  long  500
# 4     2 short  150
# 5 1 + 2  long  700
# 6 1 + 2 short  200
Run Code Online (Sandbox Code Playgroud)