Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the tilde mean in this context of R code [duplicate]

Tags:

r

tidyverse

  1. Fx=purrr::map(CDF, ~ tibble(
                        severity=severities$severity,
                        inclusive=severities$inclusive,
                        Fx=.x(severities$severity, severities$inclusive))))
    
  2. raw_df <- tibble::tribble(
        ~"segment", ~"limit", ~"attach", ~"pct_written", ~"premium", ~"product", ~"lalae_ratio",
        "",    50000,      1000,            0.5,     273456,    "prod1",           0.65,
        "",    20000,      2000,            0.5,     285760,    "prod2",           0.65,
        "",    2e+05,      3000,            0.5,     956456,    "prod3",           0.65,
        "",    10000,       300,            0.5,      90890,    "prod4",           0.65)
    

I can sort of guess it, but I do not have a precise definition of what it does.

This is different from:

dependent_variable ~ independent_variables
like image 453
Big Cat Public Safety Act Avatar asked Oct 29 '25 13:10

Big Cat Public Safety Act


1 Answers

The tilde operator in R is a general operator that creates a formula object.

However, the usage in your specific pieces of code is a special case of that: purrr co-opts formulas to implement an anonymous function notation. You can read more in the purrr introduction. But briefly, the usage

purrr::map(data, ~ expression_with(.x))

Is equivalent to

purrr::map(data, function (.x) expression_with(.x))

The second piece of code does something different still, and that usage is described in the documentation of the tribble function.

like image 91
Konrad Rudolph Avatar answered Nov 01 '25 03:11

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!