Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meaning of first tilde in purrr::map

Tags:

r

purrr

tilde

I was looking at this example that uses map. Here it is:

mtcars %>%
  split(.$cyl) %>% # from base R
  map(~ lm(mpg ~ wt, data = .))

What is the meaning of the first tilde in map(~ lm...? That is, how does R interpret the first tilde? (I understand that the second tilde indicates a function...). Another way of asking is, why doesn't the following work?

mtcars %>%
  split(.$cyl) %>% # from base R
  map(lm(mpg ~ wt, data = .))
like image 930
CPak Avatar asked Jun 29 '17 20:06

CPak


People also ask

What does tilde mean in Dplyr?

Tilde operator is used to define the relationship between dependent variable and independent variables in a statistical model formula. The variable on the left-hand side of tilde operator is the dependent variable and the variable(s) on the right-hand side of tilde operator is/are called the independent variable(s).

What does the map function from purrr do?

The map functions transform their input by applying a function to each element of a list or atomic vector and returning an object of the same length as the input. map() always returns a list. See the modify() family for versions that return an object of the same type as the input.

What does tilde mean in R?

Tilde symbol l is used within formulas of statistical models, as mainly this symbol is used to define the relationship between the dependent variable and the independent variables in the statistical model formula in the R programming language.

Is purr part of tidyverse?

Purrr is one of those tidyverse packages that you keep hearing about, and you know you should probably learn it, but you just never seem to get around to it. At it's core, purrr is all about iteration.


1 Answers

As per the map help documentation, map needs a function but it also accepts a formula, character vector, numeric vector, or list, the latter of which are converted to functions.

The ~ operator in R creates formula. So ~ lm(mpg ~ wt, data = .) is a formula. Formulas are useful in R because they prevent immediate evaluation of symbols. For example you can define

x <- ~f(a+b)

without f, a or b being defined anywhere. In this case ~ lm(mpg ~ wt, data = .) is basically a shortcut for function(x) {lm(mpg ~ wt, data = x)} because map can change the value of . in the formula as needed.

Without the tilde, lm(mpg ~ wt, data = .) is just an expression or call in R that's evaluated immediately. The . wouldn't be defined at the time that's called and map can't convert that into a function.

You can turn these formulas into functions outside of the map() with purrr::as_mapper() function. For example

myfun <- as_mapper(~lm(mpg ~ wt, data = .))
myfun(mtcars)
# Call:
# lm(formula = mpg ~ wt, data = .)
# 
# Coefficients:
# (Intercept)           wt  
#      37.285       -5.344  

myfun
# <lambda>
# function (..., .x = ..1, .y = ..2, . = ..1) 
# lm(mpg ~ wt, data = .)
# attr(,"class")
# [1] "rlang_lambda_function"

You can see how the . becomes the first parameter that's passed to that function.

like image 105
MrFlick Avatar answered Oct 04 '22 01:10

MrFlick