-1

How to do vector sum (i.e. row sum, element by element) in dplyr.

Data to play with:

> dt <- ggplot2::diamonds[1:3,5:10]  
   depth table price     x     y     z
1:  61.5    55   326  3.95  3.98  2.43
2:  59.8    61   326  3.89  3.84  2.31
3:  56.9    65   327  4.05  4.07  2.31

I'd like to add a column Total to the dt which will be equal to element by element sum of columns" cols <- 3:5

NB1: I know how to do it in data.table: dt [ , Total:= rowSums(.SD), .SDcols=cols]

NB2: In dplyr, we can do: dt %>% mutate(Total=x+y+z). But how to do it in column-name agnostic waycols=3:5 (or for cols=3:5000 ) ?

PS. This question was originally asked here: How to do vector/row sum (element by element) in data.table and dplyr?, but was closed by someone as Duplicate before it was answered.

IVIM
  • 2,167
  • 1
  • 15
  • 41
  • Why do you say that `dt [ , Total:= rowSums(.SD), .SDcols=cols]` doesn't work? – s_baldur Sep 16 '20 at 13:31
  • Please don't repost the same question. See [What is the strategy to handle users who repost their questions if previous one is unanswered](https://meta.stackoverflow.com/questions/332271/what-is-the-strategy-to-handle-users-who-repost-their-questions-if-previous-one); [“This question already has answers here” - but it does not. What can I do when I think my question's not a duplicate?](https://meta.stackoverflow.com/questions/252252/this-question-already-has-answers-here-but-it-does-not-what-can-i-do-when-i) – Henrik Sep 16 '20 at 14:32
  • 1
    The `data.table` part is a duplicate of [Summing across rows of a data.table for specific columns](https://stackoverflow.com/questions/21857679/summing-across-rows-of-a-data-table-for-specific-columns) – Henrik Sep 16 '20 at 14:33
  • I removed the part related to use of data.table . Thanks for showing how to do it in dplyr! – IVIM Sep 17 '20 at 14:14

1 Answers1

1

The dplyr options would be using c_across() or rowSums() with mutate():

Option 1:

library(dplyr)
#Data
dt <- ggplot2::diamonds[1:3,5:10] 

#Option 1
dt %>% rowwise() %>%
  mutate(sumVar = sum(c_across(price:y)))

Output:

# A tibble: 3 x 7
# Rowwise: 
  depth table price     x     y     z sumVar
  <dbl> <dbl> <int> <dbl> <dbl> <dbl>  <dbl>
1  61.5    55   326  3.95  3.98  2.43   334.
2  59.8    61   326  3.89  3.84  2.31   334.
3  56.9    65   327  4.05  4.07  2.31   335.

Option 2:

#Option 2
dt %>% mutate(sumVar = rowSums(.[3:5]))

Output:

# A tibble: 3 x 7
  depth table price     x     y     z sumVar
  <dbl> <dbl> <int> <dbl> <dbl> <dbl>  <dbl>
1  61.5    55   326  3.95  3.98  2.43   334.
2  59.8    61   326  3.89  3.84  2.31   334.
3  56.9    65   327  4.05  4.07  2.31   335.
Duck
  • 39,058
  • 13
  • 42
  • 84