1

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?

Chris
  • 820
  • 7
  • 22
  • 1
    Your code for the `get_from_df` function isn't valid, so you shouldn't be getting anything returned. – thelatemail Nov 24 '21 at 19:54
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Make sure all the code you share actually runs in R. – MrFlick Nov 24 '21 at 19:56
  • In addition to echoing MrFlick's comment regarding reproducible examples, you can look at the source code for any function by calling it directly. For example, for the function `View()`, you can see the source code in console by calling `View` (without the parentheses). To open the source code in a new script tab in RStudio, call `View(View)` . The documentation for a function can be obtained by calling `?View` – Jessica Burnett Nov 24 '21 at 20:04
  • 2
    Here are also some references on rowwise: https://dplyr.tidyverse.org/articles/rowwise.html. – deschen Nov 24 '21 at 20:06
  • So you are just trying to sum those columns per row? The more idomatic dplyr verb for that is `c_across`: `df %>% rowwise() %>% mutate(presum=sum(c_across(all_of(l))))` – MrFlick Nov 24 '21 at 20:07
  • @MrFlick That is in fact the immediate need. The question is about a good source for finding these answers. – Chris Nov 24 '21 at 20:08
  • @deschen Thanks for the useful link and please post your comment as an answer. – Chris Nov 24 '21 at 22:43

1 Answers1

1

Perhaps this is what you looking for?

Note the use of c_across and all_of from tidy selection.

library(tidyverse)

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

df %>% 
  rowwise() %>% 
  mutate(presum = sum(c_across(all_of(l))))
#> # A tibble: 10 × 3
#> # Rowwise: 
#>        x     y presum
#>    <int> <int>  <int>
#>  1     1    21     22
#>  2     2    22     24
#>  3     3    23     26
#>  4     4    24     28
#>  5     5    25     30
#>  6     6    26     32
#>  7     7    27     34
#>  8     8    28     36
#>  9     9    29     38
#> 10    10    30     40

Created on 2021-11-24 by the reprex package (v2.0.1)

jpdugo17
  • 6,816
  • 2
  • 11
  • 23