R.Bushroot R.Bushroot - 2 months ago 14
R Question

Calulate row sums with dplyr omitting a column by its name

Using dplyr, I would like to calculate row sums across all columns exept one.
I managed to do that by using the column index.
However, I would like to use the column name instead of the column index.
How can I do that?

Example data:

# Using dplyr 0.5.0
library(tidyverse)

# Create example data
`Urban/Rural` <- c("rural", "urban")
type1 <- c(1582, 671)
type2 <- c(5247, 4123)
type3 <- c(87, 65)
df <- data.frame(`UrbanRural`, type1, type2, type3)
df <- tbl_df(df)




# A tibble: 2 x 5
UrbanRural type1 type2 type3 tot
<fctr> <dbl> <dbl> <dbl> <dbl>
1 rural 1582 5247 87 6916
2 urban 671 4123 65 4859


Example that works (using the column index):

df %>% mutate(tot = rowSums(.[-1]))




# A tibble: 2 x 5
UrbanRural type1 type2 type3 tot
<fctr> <dbl> <dbl> <dbl> <dbl>
1 rural 1582 5247 87 6916
2 urban 671 4123 65 4859


Example of what I would like to do:

df %>% mutate(tot = rowSums(select(., -UrbanRural)))

Answer

We can use setdiff to select columns except "UrbanRural"

df %>%
   mutate(tot = rowSums(.[setdiff(names(.), "UrbanRural")]))
#   UrbanRural type1 type2 type3   tot
#       <fctr> <dbl> <dbl> <dbl> <dbl>
#1      rural  1582  5247    87  6916
#2      urban   671  4123    65  4859

If we want to use select

df %>% 
   select(-one_of("UrbanRural")) %>% 
   rowSums() %>% 
   cbind(df, tot = .) 
#   UrbanRural type1 type2 type3   tot
# 1      rural  1582  5247    87  6916
# 2      urban   671  4123    65  4859
Comments