Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading