1
votes

I have been trying unsuccessfully to extract the name of a variable that was passed to a function in dplyr::mutate(). Below is a short example where I want to create a function that returns the string "mpg" inside mutate:

# mtcars dataset with grouping variable
dataset = mtcars
dataset$group = c(1, 2, 3, 4)

# function to call inside mutate()
f = function(col, data){
  str_col = deparse(lazyeval::expr_find(col))
  str_col
}

# this does not work: returns the content of mpg 
# instead of the variable name as a string
dataset %>%
  group_by(group) %>%
  mutate(f = f(mpg, dataset)
  ) %>%
  select(group, f)

I used lazyeval::expr_find(), because subsitute only "goes up" one layer as far as I understood the documentation. It works when I call f() inside the function wrap(), but it returns the content of mpg, instead of the name "mpg" when I put it inside group_by()%>%mutate()

I found some questions that are related, but none of them provided a solution to my problem.

Any help greatly appreciated:)

1
f_updated is simply going to produce a value of 1. It looks like you are simply trying to divide the group sum by the total sum? if so try this: dataset %>% dplyr::select(mpg, group) %>% mutate(tot.sum=sum(mpg)) %>% group_by(group) %>% summarise(result = sum(mpg)/mean(tot.sum)) - B Williams
I fear my text was a bit misleading, sry... sum(mpg)/sum(.$mpg) already does the trick, but that was just a small example to check if I get the same output. My problem is that I want get the variable name as a string when I pass it to a function in mutate() and that is not working yet:( thx though! - Christoph
yeah i don't have any idea what your actual question is - B Williams
Thx @BWilliams for your feedback! I edited the question and hope it is clearer now. - Christoph

1 Answers

1
votes

I'm still not entirely clear on what you are trying to do, but maybe this helps:

f = function(col, data){
  str_col = deparse(substitute(col))
  data.frame(str_col)
}

dataset %>% 
   group_by(group) %>% 
   do(f(mpg, .))