将行添加到数据框中,并包含组内数据的总和 [英] Add row to dataframe with sum of within group data

查看:45
本文介绍了将行添加到数据框中,并包含组内数据的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个示例数据框.

I have an example dataframe below.

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

我需要为每个时间段创建总计size值的行.我已经查看了聚合和依据的组合,但是无法使其正常工作.

I need to create rows which total the values of size, for each time period. I have looked at combinations of aggregate and by, but I cannot get it to work correctly.

我尝试过的例子:

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

我希望最终数据帧看起来像一个例子:

An example of what I want the final dataframe to look like:

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为底的解决方案将不胜感激.

Any help is appreciated, a solution with base R would be really appreciated.

推荐答案

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

这篇关于将行添加到数据框中,并包含组内数据的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆