I am self-studying R. Below is a silly question:
mtcars %>%
split(.$cyl) %>%
map(~ lm(mpg ~ wt, data = .x))
I understand everything except the '.x'. What does it means?
Thanks!
I am self-studying R. Below is a silly question:
mtcars %>%
split(.$cyl) %>%
map(~ lm(mpg ~ wt, data = .x))
I understand everything except the '.x'. What does it means?
Thanks!
.x
refers to the dataframe in each list.
tmp <- mtcars %>% split(.$cyl)
So for 1st iteration .x
would be tmp[[1]]
, for second tmp[[2]]
and so on. Note that instead of .x
you can also use .
here which would return the same output.
See documentation in ?map
:
There are three ways to refer to the arguments:
For a single argument function, use .
For a two argument function, use .x and .y
For more arguments, use ..1, ..2, ..3 etc