How does dplyr's select helper function everything() differ from copying?

后端 未结 2 1129
萌比男神i
萌比男神i 2020-12-31 18:11

What is the use case for

select(iris, everything())

as opposed to e.g. just copying the data.frame?

2条回答
  •  独厮守ぢ
    2020-12-31 18:33

    Another example use case:

    # Moves the variable Petal.Length to the end
    select(iris, -Petal.Length, everything())
    

    (I saw it here: https://stackoverflow.com/a/30472217/4663008)

    Either way, both Gregor's answer and mine are confusing to me - I would have expected Species to be duplicated in Gregor's example or removed in my example. e.g. if you try something more complicated based on the previous two examples, it doesn't work:

    > dplyr::select(iris, Petal.Width, -Petal.Length, everything())
        Petal.Width Sepal.Length Sepal.Width Petal.Length    Species
    1           0.2          5.1         3.5          1.4     setosa
    2           0.2          4.9         3.0          1.4     setosa
    3           0.2          4.7         3.2          1.3     setosa
    

    Update: After a quick response from hadley on github, I found out that there is a special behaviour using everything() combined with a negative in the first position in select() that will start select() off with all the variables and then everything() draws them back out again. A negative variable in non-first positions do not work as one might expect.

    I agree that the negative variable in first position and the everything() select_helper function needs to be better explained in the documentation

    Update 2: the documentation for ?select has now been updated to state "Positive values select variables; negative values to drop variables. If the first expression is negative, select() will automatically start with all variables."

提交回复
热议问题