繁体   English   中英

具有某些名称的列的行总和

[英]Row-wise sum for columns with certain names

我有一个样本数据:

SampleID  a      b     d     f       ca      k     l    cb
1         0.1    2     1     2       7       1     4    3
2         0.2    3     2     3       4       2     5    5
3         0.5    4     3     6       1       3     9    2

我需要找到在名称上具有某些共同点的列的按行sum(a, ca) ,例如按行sum(a, ca)或按行sum(b,cb) 问题是我有很大的data.frame,理想情况下,我将能够写出列标题中常见的内容,因此代码只选择那些列求和

预先感谢您的协助。

我们可以使用grep选择具有'a'的列,对这些列进行子集化,并执行rowSums ,而对于'b'列进行选择。

 rowSums(df1[grep('a', names(df1)[-1])+1])
 rowSums(df1[grep('b', names(df1)[-1])+1])

如果要将输出作为数据帧,请尝试使用dplyr

# Recreating your sample data
df <- data.frame(SampleID = c(1, 2, 3),
             a = c(0.1, 0.2, 0.5),
             b = c(2, 3, 4),
             d = c(1, 2, 3),
             f = c(2, 3, 6),
             ca = c(7, 4, 1),
             k = c(1, 2, 3),
             l = c(4, 5, 9),
             cb = c(3, 5, 2)) 

处理数据

# load dplyr
library(dplyr)

# Sum across columns 'a' and 'ca' (sum(a, ca))
df2 <- df %>%
    select(contains('a'), -SampleID) %>% # 'select' function to choose the columns you want 
    mutate(row_sum = rowSums(.)) # 'mutate' function to create a new column 'row_sum' with the sum of the selected columns. You can drop the selected columns by using 'transmute' instead.

df2 # have a look

    a ca row_sum
1 0.1  7     7.1
2 0.2  4     4.2
3 0.5  1     1.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM