Instead of explicitly stating the column names in a mutate call, I want to do so dynamically from a list of names generated elsewhere. I tried something along these lines:
df <- data.frame(x=1:10, y=21:30)
l <- c('x', 'y')
get_from_df <- function(x) { get(pos=df,x) }
df %>% rowwise %>% mutate(presum=do.call(sum, map(l, get_from_df)))
It gives the sum for the whole data.frame, 310, not the row sums. output is:
df %>% rowwise %>% mutate(presum=do.call(sum, map(l, get_from_df)))
# A tibble: 10 x 3
# Rowwise:
x y presum
<int> <int> <int>
1 1 21 310
2 2 22 310
3 3 23 310
4 4 24 310
5 5 25 310
6 6 26 310
7 7 27 310
8 8 28 310
9 9 29 310
10 10 30 310
I ended up back without rowwise using rowSum instead.
df %>% mutate(presum=rowSums(do.call(cbind, map(l,f)) ))
It works, but I'd like to understand better what rowwise is doing under the covers and what limitations I ran into. Is there anywhere that describes this before I make time to read some code?