Reputation: 1293
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))
Upvotes: 2
Views: 1284
Reputation: 388907
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
Upvotes: 1
Reputation: 8413
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())
Upvotes: 6