Antti Antti - 9 days ago 6
R Question

Summing over variables with certain pattern in dplyr mutate

I have a data.frame with several variables that I need to sum based on a pattern in their name. More specifically I have shares that sum up to one, excluding a possible residual which I need to find out. I'm using

dplyr
for this.

A sample data.frame:

df <- data.frame(year = c(2000, 2001, 2002),
aShare = c(.1,.2,.3),
bShare = c(.3,.4,.5))


I have tried to use
ends_with
function like this:

tmp <- df %>% mutate(otherShare = 1 - sum(ends_with("Share")))


But it does not produce the needed outcome:

TMP <- df %>% mutate(otherShare = 1 - (aShare + bShare))

Answer

With base R

df$x <-1- rowSums(df[colnames(df)[grepl("Share",colnames(df))]])

With semi-dplyr :P

df$x = (1-df %>% select(ends_with("Share")) %>% rowSums())