Search code examples
rfunctionmagrittr

R function does not work when used in pipe


I have made this function:

library(tibble); library(dplyr); library(magrittr)

ddes <- function(variable) {
  
  n = length(variable)
  median = median(variable)
  mean = mean(variable)
  sd = sd(variable)
  se = round(sqrt(var(variable) / length(variable)), 2)
  lower.95 = mean(variable) - (abs(qt(0.025, df = n - 1)) * se)
  upper.95 = mean(variable) + (abs(qt(0.025, df = n - 1)) * se)
  
  tibble(n, median, mean, sd, se, lower.95, upper.95)
  
}

It works fine on its own:

ddes(mtcars$mpg)

# # A tibble: 1 × 7
# n median  mean    sd    se lower.95 upper.95
# <int>  <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
#   1    32   19.2  20.1  6.03  1.07     17.9     22.3

But when used in pipe it fails. How can I use with pipe?

mtcars %>% ddes(mpg)

# Error in ddes(., mpg) : unused argument (mpg)

Solution

  • The function called after %>% is supposed to have a data argument (or with another name) in first position, which is why you see ddes(., mpg) in the error message. You could rewrite your function like that:

    library(tibble)
    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    
    ddes <- function(data, variable) {
      
      mydata <- pull(data, {{ variable }})
      n = length(mydata)
      median = median(mydata)
      mean = mean(mydata)
      sd = sd(mydata)
      se = round(sqrt(var(mydata) / length(mydata)), 2)
      lower.95 = mean(mydata) - (abs(qt(0.025, df = n - 1)) * se)
      upper.95 = mean(mydata) + (abs(qt(0.025, df = n - 1)) * se)
      
      tibble(n, median, mean, sd, se, lower.95, upper.95)
      
    }
    
    mtcars %>% ddes(mpg)
    #> # A tibble: 1 × 7
    #>       n median  mean    sd    se lower.95 upper.95
    #>   <int>  <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
    #> 1    32   19.2  20.1  6.03  1.07     17.9     22.3
    

    See more details in the Programming with dplyr vignette.