0

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!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Steve
  • 193
  • 8
  • 1
    Does this answer your question? [What does the dplyr period character "." reference?](https://stackoverflow.com/questions/35272457/what-does-the-dplyr-period-character-reference) – mhovd Dec 03 '20 at 10:48
  • @mhovd No, that’s a different, unrelated usage. – Konrad Rudolph Dec 03 '20 at 10:54

1 Answers1

0

.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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213