What is the difference between . and .data?

前端 未结 4 2042
挽巷
挽巷 2021-02-09 03:46

I\'m trying to develop a deeper understanding of using the dot (".") with dplyr and using the .data pronoun with dplyr. The code

4条回答
  •  日久生厌
    2021-02-09 04:03

    On a theoretical level:

    . is the magrittr pronoun. It represents the entire input (often a data frame when used with dplyr) that is piped in with %>%.

    .data is the tidy eval pronoun. Technically it is not a data frame at all, it is an evaluation environment.

    On a practical level:

    . will never be modified by dplyr. It remains constant until the next piped expression is reached. On the other hand, .data is always up to date. That means you can refer to previously created variables:

    mtcars %>%
      mutate(
        cyl2 = cyl + 1,
        am3 = .data[["cyl2"]] + 10
      )
    

    And you can also refer to column slices in the case of a grouped data frame:

    mtcars %>%
      group_by(cyl) %>%
      mutate(cyl2 = .data[["cyl"]] + 1)
    

    If you use .[["cyl"]] instead, the entire data frame will be subsetted and you will get an error because the input size is not the same as the group slice size. Tricky!

提交回复
热议问题