Céline
Céline

Reputation: 17

Sum of columns with similar column name

I have multiple columns. Some of them contain a certain string, say "ABC DEF".

I need the sum of the columns containing this string (I'm working with dplyr).

df <- data.frame("aaa" = 2:5, "bbb" = 1:4, "ABC_DEF" = 1:4, "DEF" = 2:5, "ABC_DEF_GHI" = 3:6, "aaa_ABC_DEF" = 2:5)

  aaa bbb ABC_DEF DEF ABC_DEF_GHI aaa_ABC_DEF
1   2   1       1   2           3           2
2   3   2       2   3           4           3
3   4   3       3   4           5           4
4   5   4       4   5           6           5

I tried something like this:

df %>% 
  mutate(ABC_DEF = sum(select(c(contains("ABC_DEF")))))

With this I get the error : ! contains() must be used within a selecting function.

Desired output:

 aaa bbb ABC_DEF_G DEF ABC_DEF_GHI aaa_ABC_DEF ABC_DEF
1   2   1         1   2           3           2       6
2   3   2         2   3           4           3       9
3   4   3         3   4           5           4      12
4   5   4         4   5           6           5      15

Can anyone help me how I could do it?

Upvotes: 0

Views: 65

Answers (2)

YH Jang
YH Jang

Reputation: 1348

With dplyr and rowSums(),

require(dplyr)
df <- data.frame("aaa" = 2:5, "bbb" = 1:4, "ABC DEF" = 1:4, "DEF" = 2:5, "ABC DEF GHI" = 3:6, "aaa ABC DEF" = 2:5)

df %>% 
  select(contains('ABC.DEF')) %>%  
  mutate(ABC.DEF.SUM = rowSums(across(everything())))

Output

  ABC.DEF ABC.DEF.GHI aaa.ABC.DEF ABC.DEF.SUM
1       1           3           2           6
2       2           4           3           9
3       3           5           4          12
4       4           6           5          15

Upvotes: 1

benson23
benson23

Reputation: 19097

You can use a combination of rowwise and c_across to do the job.

library(dplyr)

df %>% rowwise() %>% 
  mutate(ABC.DEF.1 = sum(c_across(contains("ABC.DEF")))) %>% 
  ungroup()

# A tibble: 4 × 7
    aaa   bbb ABC.DEF   DEF ABC.DEF.GHI aaa.ABC.DEF ABC.DEF.1
  <int> <int>   <int> <int>       <int>       <int>     <int>
1     2     1       1     2           3           2         6
2     3     2       2     3           4           3         9
3     4     3       3     4           5           4        12
4     5     4       4     5           6           5        15

Upvotes: 1

Related Questions