What is the difference between . and .data?

前端 未结 4 2034
挽巷
挽巷 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:06

    Compare mtcars %>% count(.data[["cyl"]]) vs. mtcars %>% count(.[["cyl"]]).

    mtcars %>% count(.[["cyl"]])
      .[["cyl"]]  n
    1          4 11
    2          6  7
    3          8 14
    
    
    mtcars %>% count(.data[["cyl"]])
      cyl  n
    1   4 11
    2   6  7
    3   8 14
    

    . is literally just the previous result. So the first is similar to:

    . <- mtcars
    count(., .[["cyl"]])
    

    The second is a shorthand for looking up the variable by the string "cyl" and treating the previous result as the search path for the variable. For example, suppose you mispelled your variable name:

    mtcars %>% count(.[["cyll"]])
       n
    1 32
    
    mtcars %>% count(.data[["cyll"]])
    Error: Must group by variables found in `.data`.
    * Column `cyll` is not found.
    

    Using . will not throw an error because indexing to a non-existing column is a valid base-R operation that returns NULL.

    Using .data will throw because using a non-existent variable:

    mtcars %>% count(cyll)
    

    Also throws.

提交回复
热议问题