1

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

Christoph
  • 575
  • 4
  • 15
  • 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 Mar 08 '17 at 21:08
  • 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 Mar 09 '17 at 17:44
  • 1
    yeah i don't have any idea what your actual question is – B Williams Mar 09 '17 at 18:57
  • Thx @BWilliams for your feedback! I edited the question and hope it is clearer now. – Christoph Mar 10 '17 at 18:29

1 Answers1

1

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, .))
B Williams
  • 1,992
  • 12
  • 19
  • Thx, sorry for the confusion... What I want to do is to pass function f a column name and a data frame inside mutate. I want to parse the column name into a string (because dplyr uses non-standard-evaluation) and then use it to do data frame[, col_name_as_string] so I can work with the original data frame columns inside group_by Basically, I want to call this function: f = function(col, data){ str_col = deparse(substitute(col)) sum(col)/data[,str_col] } but that does not work unfortunately, because the deparse(substitute()) part is not returning the variable name inside mutate – Christoph Mar 13 '17 at 08:25