1

I am trying to find a way to summarise multiple columns column-wise in R by specifying the column that I don't want to summarise. For example in the following case I want do summarise all columns that are not named "cat" or "type":

This data frame:

df <- data_frame(cat = c('A', 'B', 'C'), 
                 val1 = c(0.1, 0.3, 0.5), 
                 val2 = c(0.2, 0.2, 0.7), 
                 val3 = c(0.14, 0.31, 0.35),
                 type = c(4, 2, 5))

To this one:

df <- data_frame(cat = c('A', 'B', 'C'), 
                 val1 = c(0.1, 0.3, 0.5), 
                 val2 = c(0.2, 0.2, 0.7), 
                 val3 = c(0.14, 0.31, 0.35),
                 type = c(4, 2, 5),
                 sum = c(0.44,0.81,1.55))

I have been trying using dplyr mutate functions to do this but I cannot figure out how to do it.

Any suggestion of how to do this would be super appreciated.

Vibramat
  • 187
  • 9

3 Answers3

5

You can remove the columns that you don't want using select :

library(dplyr)
df %>% mutate(sum = rowSums(select(., -cat, -type)))

#  cat    val1  val2  val3  type   sum
#  <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 A       0.1   0.2  0.14     4  0.44
#2 B       0.3   0.2  0.31     2  0.81
#3 C       0.5   0.7  0.35     5  1.55
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

You may also use ! bang operator in dplyr's summarise(c_across(.. alongwith rowwise like this


library(dplyr)

df %>% rowwise() %>%
  mutate(sum = sum(c_across(!c(cat, type))))
#> # A tibble: 3 x 6
#> # Rowwise: 
#>   cat    val1  val2  val3  type   sum
#>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 A       0.1   0.2  0.14     4  0.44
#> 2 B       0.3   0.2  0.31     2  0.81
#> 3 C       0.5   0.7  0.35     5  1.55

Created on 2021-04-29 by the reprex package (v2.0.0)

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
1

In base R you could do:

Update: thanks to AnilGoyal for clarification

To remove certain columns (in your case col1=cat and col5=type) in base R you could do:

df$sum <- rowSums(df[, c(-1,-5)])

First answer:

df$sum <- rowSums(df[2:4])

Output:

  cat    val1  val2  val3  type   sum
  <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A       0.1   0.2  0.14     4  0.44
2 B       0.3   0.2  0.31     2  0.81
3 C       0.5   0.7  0.35     5  1.55
TarJae
  • 72,363
  • 6
  • 19
  • 66