0

I'd like to have the sum of absolute values of multiple columns with certain characteristics, say their names end in _s.

set.seed(154)
d <- data.frame(a_s = sample(-10:10,6,replace=F),b_s = sample(-5:10,6,replace=F), c = sample(-10:5,6,replace=F))
d$s <- abs(d$a_s)+abs(d$b_s)

where the output is column s below:

  a_s b_s  c  s
    4   8 -2 12
   10   6 -8 16
  -10  -1  1 11
    0   2  4  2
    5   1 -3  6
    8  -5  5 13

I can use d$ss <- rowSums(d[,grepl('_s',colnames(d))]) to sum the values but not the absolute values.

Ana
  • 1,516
  • 3
  • 15
  • 26

0 Answers0