并用它们的总和创建新列

并用它们的总和创建新列

本文介绍了按组对多个变量求和,并用它们的总和创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有分组变量的数据框,我想按组对其求和.使用 dplyr 很容易.

I have a data frame with grouped variable and I want to sum them by group. It's easy with dplyr.

library(dplyr)
library(magrittr)

data <- data.frame(group = c("a", "a", "b", "c", "c"), n1 = 1:5, n2 = 2:6)

data %>% group_by(group) %>%
  summarise_all(sum)

# A tibble: 3 x 3
   group    n1    n2
  <fctr> <int> <int>
1      a     3     5
2      b     3     4
3      c     9    11

但是现在我想要一个新列 total ,其中按组分别包含 n1 n2 的总和.像这样:

But now I want a new column total with the sum of n1 and n2 by group. Like this:

# A tibble: 3 x 3
   group    n1    n2   ttl
  <fctr> <int> <int> <int>
1      a     3     5     8
2      b     3     4     7
3      c     9    11    20

如何使用 dplyr 来做到这一点?

How can I do that with dplyr?

实际上,这只是一个例子,我有很多变量.

Actually, it's just an example, I have a lot of variables.

我尝试了这两个代码,但是尺寸不合适...

I tried these two codes but it's not in the right dimension...

data %>% group_by(group) %>%
  summarise_all(sum) %>%
  summarise_if(is.numeric, sum)

data %>% group_by(group) %>%
  summarise_all(sum) %>%
  mutate_if(is.numeric, .funs = sum)

推荐答案

您可以在总结之后使用 mutate :

data %>%
    group_by(group) %>%
    summarise_all(sum) %>%
    mutate(tt1 = n1 + n2)

# A tibble: 3 x 4
#   group    n1    n2   tt1
#  <fctr> <int> <int> <int>
#1      a     3     5     8
#2      b     3     4     7
#3      c     9    11    20


如果需要对所有数字列求和,可以将 rowSums select_if (用于选择数字列)一起使用以求和:


If need to sum all numeric columns, you can use rowSums with select_if (to select numeric columns) to sum columns up:

data %>%
    group_by(group) %>%
    summarise_all(sum) %>%
    mutate(tt1 = rowSums(select_if(., is.numeric)))

# A tibble: 3 x 4
#   group    n1    n2   tt1
#  <fctr> <int> <int> <dbl>
#1      a     3     5     8
#2      b     3     4     7
#3      c     9    11    20

这篇关于按组对多个变量求和,并用它们的总和创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:08