简体   繁体   中英

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))

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())

Not probably the best option, but we can use apply row-wise

df$otherShare <- apply(df[grep("Share$", names(df))], 1, function(x) 1 - sum(x))

#   year aShare bShare otherShare
#1 2000    0.1    0.3        0.6
#2 2001    0.2    0.4        0.4
#3 2002    0.3    0.5        0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM