New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent mutate() from updating columns sequentially
#4517
Comments
mapping()mutate() from updating columns sequentially
|
I think the title on this could be a little clearer since sequential update is an intrinsic feature of mutate. What you're describing is an optional parameter to Solutions using |
|
That's another case for library(dplyr, warn.conflicts = FALSE)
as_tibble(mtcars) %>%
mutate(across(everything(), ~ .x / sd(disp)))
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.169 0.0484 1.29 0.888 0.0315 0.0211 0.133 0 0.00807 0.0323
#> 2 0.169 0.0484 1.29 0.888 0.0315 0.0232 0.137 0 0.00807 0.0323
#> 3 0.184 0.0323 0.871 0.750 0.0311 0.0187 0.150 0.00807 0.00807 0.0323
#> 4 0.173 0.0484 2.08 0.888 0.0249 0.0259 0.157 0.00807 0 0.0242
#> 5 0.151 0.0645 2.90 1.41 0.0254 0.0278 0.137 0 0 0.0242
#> 6 0.146 0.0484 1.82 0.847 0.0223 0.0279 0.163 0.00807 0 0.0242
#> 7 0.115 0.0645 2.90 1.98 0.0259 0.0288 0.128 0 0 0.0242
#> 8 0.197 0.0323 1.18 0.500 0.0298 0.0257 0.161 0.00807 0 0.0323
#> 9 0.184 0.0323 1.14 0.767 0.0316 0.0254 0.185 0.00807 0 0.0323
#> 10 0.155 0.0484 1.35 0.992 0.0316 0.0278 0.148 0.00807 0 0.0323
#> # … with 22 more rows, and 1 more variable: carb <dbl>Created on 2019-11-26 by the reprex package (v0.3.0.9000) |
|
Closing since we'll retire the existing scoped variants (#4702) so this will no longer be an issue. |
Inlined lambdas can refer to existing columns:
However as you can see this feature is currently hard to use because the input tibble is mutated as the map progresses. Here we're dividing all columns up to
dispby its standard deviation. After that,sd(disp)is 1.This is consistent with
mutate(), since mapping a function just templates an expression over the columns of a data frame, but makes it hard to reason about. It also makes certain patterns hard to do (https://community.rstudio.com/t/dplyr-scoped-verbs-questions-about-scoped-verbs-sequential-execution/36609/5).Could
mutate()andsummarise()have a parameter that prevents columns from being updated until the end? It should be useful withmapping(), scoped verbs, and the speculative superstache operator{{{for templating expressions.The text was updated successfully, but these errors were encountered: