What is the difference between . and .data?

前端 未结 4 2036
挽巷
挽巷 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 03:57

    The . variable comes from magrittr, and is related to pipes. It means "the value being piped into this expression". Normally with pipes, the value from a previous expression becomes argument 1 in the next expression, but this gives you a way to use it in some other argument.

    The .data object is special to dplyr (though it is implemented in the rlang package). It does not have any useful value itself, but when evaluated in the dplyr "tidy eval" framework, it acts in many ways as though it is the value of the dataframe/tibble. You use it when there's ambiguity: if you have a variable with the same name foo as a dataframe column, then .data$foo says it is the column you want (and will give an error if it's not found, unlike data$foo which will give NULL). You could alternatively use .env$foo, to say to ignore the column and take the variable from the calling environment.

    Both .data and .env are specific to dplyr functions and others using the same special evaluation scheme, whereas . is a regular variable and can be used in any function.

    Edited to add: You asked why names(.data) didn't work. If @r2evans excellent answer isn't enough, here's a different take on it: I suspect the issue is that names() isn't a dplyr function, even though names.rlang_fake_data_pronoun is a method in rlang. So the expression names(.data) is evaluated using regular evaluation instead of tidy evaluation. The method has no idea what dataframe to look in, because in that context there isn't one.

提交回复
热议问题