Row-wise sum for columns with certain names

感情迁移 提交于 2021-01-28 06:34:33

问题


I have a sample data:

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

I need to find row-wise sum of columns which have something common in names, e.g. row-wise sum(a, ca) or row-wise sum(b,cb). The problem is that i have large data.frame and ideally i would be able to write what is common in column header, so that code would pick only those columns to sum

Thank you beforehand for any assistance.


回答1:


We can select the columns that have 'a' with grep, subset the columns and do rowSums and the same with 'b' columns.

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



回答2:


If you want the output as a data frame, try using 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)) 

Process the data

# 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


来源:https://stackoverflow.com/questions/35693876/row-wise-sum-for-columns-with-certain-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!