2
votes
test <- data.frame('prod_id'= c("shoe", "shoe", "shoe", "shoe", "shoe", "shoe", "boat", "boat","boat","boat","boat","boat"), 
               'seller_id'= c("a", "b", "c", "d", "e", "f", "a","g", "h", "r", "q", "b"), 
               'Dich'= c(1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0),
               'price' = c(120, 20, 10, 4, 3, 4, 30, 43, 56, 88, 75, 44)
                )
test

       prod_id seller_id Dich price
 1     shoe         a    1   120
 2     shoe         b    0    20
 3     shoe         c    0    10
 4     shoe         d    0     4
 5     shoe         e    0     3
 6     shoe         f    0     4
 7     boat         a    0    30
 8     boat         g    0    43
 9     boat         h    1    56
10     boat         r    0    88
11     boat         q    0    75
12     boat         b    0    44

I wanted to create a new column that takes the difference between observations in the price column based on the value of Dich where each observation takes its difference from the observation where Dich==1 within each prod_id group. The syntax for doing that is below.

test %>% 
group_by(prod_id) %>% 
mutate(diff_p = if(any(Dich ==1)) price - price[Dich == 1] else NA)

       prod_id seller_id Dich price diff_p
 1     shoe         a    1   120      0
 2     shoe         b    0    20     -100
 3     shoe         c    0    10     -110
 4     shoe         d    0     4     -116
 5     shoe         e    0     3     -117
 6     shoe         f    0     4     -116
 7     boat         a    0    30     -26
 8     boat         g    0    43     -13
 9     boat         h    1    56       0
10     boat         r    0    88      32
11     boat         q    0    75      19
12     boat         b    0    44     -12

Now I would like to create a function that uses the same syntax where I can use the function on a new dataframe and get the same results. However, when I am trying the newly created column has only NA values. I'm thinking its something with the use of mutate within a function?

trans <- function(e) {e %>%
         group_by(prod_id) %>% 
         mutate(diff_p = if(any(Dich ==1)) price -price[Dich == 1] else NA)
         }
1
trans(test) works for me. Try it again on a fresh session. - G. Grothendieck
@G.Grothendieck thanks for your reply, please see my response below to Akrun if you'd like to comment too. Thanks! - Kreitz Gigs

1 Answers

2
votes

An option would be to make use of quosure and evaluate (!!)

library(tidyverse)
trans <- function(dat, groupCol, valCol1, valCol2) {
  groupCol <- enquo(groupCol)
  valCol1 <- enquo(valCol1)
  valCol2 <- enquo(valCol2)
  dat %>%
     group_by(!! groupCol) %>% 
     mutate(diff_p = if(any((!! valCol1) ==1)) (!!valCol2) - 
                 (!!valCol2)[(!!valCol1) == 1] else NA)
     }
trans(test, prod_id, Dich, price)
# A tibble: 12 x 5
# Groups:   prod_id [2]
#   prod_id seller_id  Dich price diff_p
#   <fct>   <fct>     <dbl> <dbl>  <dbl>
# 1 shoe    a             1   120      0
# 2 shoe    b             0    20   -100
# 4 shoe    d             0     4   -116
# 5 shoe    e             0     3   -117
# 6 shoe    f             0     4   -116
# 7 boat    a             0    30    -26
# 8 boat    g             0    43    -13
# 9 boat    h             1    56      0
#10 boat    r             0    88     32
#11 boat    q             0    75     19
#12 boat    b             0    44    -12

NOTE: It may be more generalizable to have column names passed as arguments for applying the function to other datasets