R:使用dplyr逐行汇总一系列列 [英] R: Summing a sequence of columns row-wise with dplyr

查看:41
本文介绍了R:使用dplyr逐行汇总一系列列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本着类似问题的精神,在此处此处,我希望能够对 data_frame &创建一个新列:

In the spirit of similar questions along these lines here and here, I would like to be able to sum across a sequence of columns in my data_frame & create a new column:

df_abc = data_frame(
  FJDFjdfF = seq(1:100),
  FfdfFxfj = seq(1:100),
  orfOiRFj = seq(1:100),
  xDGHdj = seq(1:100),
  jfdIDFF = seq(1:100),
  DJHhhjhF = seq(1:100),
  KhjhjFlFLF = seq(1:100),
  IgiGJIJFG= seq(1:100),
)

# this does what I want
df_abc %>% 
  mutate(
    sum_1 = orfOiRFj + xDGHdj + jfdIDFF + DJHhhjhF
  ) 

很显然,如果此序列中有很多变量,则将它们键入出来是不可行的.同样,变量的名称也不是正则表达式友好的,因此除了它们按顺序出现之外,不能通过规则选择.

Clearly, if there are a lot of variables in this sequence, typing them out is not feasible. Also, the names of the variables are not regex-friendly, so cannot be selected by a rule, other than the fact that they occur in a sequence.

我希望tidyverse中存在一个抽象,该抽象允许以下内容:

I am hoping that there exists an abstraction in the tidyverse, that allows something like:

df_abc %>% 
  mutate(
    sum_1 = sum(orfOiRFj:DJHhhjhF)
  )

谢谢.

推荐答案

您可以使用 rowSums 来做到这一点:

You can use rowSums to do that:

# option 1
df_abc %>% mutate(sum_1 = rowSums(.[3:6]))
# option 2
df_abc %>% mutate(sum_1 = rowSums(select(.,orfOiRFj:DJHhhjhF)))

结果:

# A tibble: 100 x 9
   FJDFjdfF FfdfFxfj orfOiRFj xDGHdj jfdIDFF DJHhhjhF KhjhjFlFLF IgiGJIJFG sum_1
      <int>    <int>    <int>  <int>   <int>    <int>      <int>     <int> <dbl>
 1        1        1        1      1       1        1          1         1     4
 2        2        2        2      2       2        2          2         2     8
 3        3        3        3      3       3        3          3         3    12
 4        4        4        4      4       4        4          4         4    16
 5        5        5        5      5       5        5          5         5    20
 6        6        6        6      6       6        6          6         6    24
 7        7        7        7      7       7        7          7         7    28
 8        8        8        8      8       8        8          8         8    32
 9        9        9        9      9       9        9          9         9    36
10       10       10       10     10      10       10         10        10    40
# ... with 90 more rows

这篇关于R:使用dplyr逐行汇总一系列列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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