Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum column with similar name

Tags:

r

sum

I have a Dataset in R like this (my real dataset has more rows and columns):

AB1 AB3 AB4 XB1 XB3 XB4
12 34 0 5 3 7

I need to sum the column similar like

AB1+XB1 AB3+XB3 AB4+XB4

What is the code I can use?

like image 622
Inuraghe Avatar asked Oct 23 '25 11:10

Inuraghe


1 Answers

You could use

library(dplyr)
df %>% 
  mutate(across(starts_with("AB"),
                ~.x + df[[gsub("AB", "XB", cur_column())]],
                .names = "sum_{.col}"))

This returns

# A tibble: 1 x 9
    AB1   AB3   AB4   XB1   XB3   XB4 sum_AB1 sum_AB3 sum_AB4
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>
1    12    34     0     5     3     7      17      37       7
  • We use across and mutate in this approach.
  • First we select all columns starting with AB. The desired sums are always ABn + XB2, so we can use this pattern.
  • Next we replace AB in the name of the current selected column with XB and sum those two columns. These sums are stored in a new column prefixed with sum_.
like image 170
Martin Gal Avatar answered Oct 25 '25 00:10

Martin Gal