Hi,
I have a question about the new version of dplyr. In the new version of dplyr::summarize()
, one can create multiple columns at once. Is that possible through the new dplyr::mutate()
too?
For example, lets say I had a table with sales by day:
library(dplyr, warn.conflicts = FALSE)
# this next line creates the table
df1 <- tibble::tribble(
~day, ~sales,
1L, 260L,
2L, 230L,
3L, 110L,
4L, 420L,
5L, 420L,
6L, 380L,
7L, 360L,
8L, 860L,
9L, 300L,
10L, 240L
)
# The next few lines show the table we created earlier
df1
#> # A tibble: 10 x 2
#> day sales
#> <int> <int>
#> 1 1 260
#> 2 2 230
#> 3 3 110
#> 4 4 420
#> 5 5 420
#> 6 6 380
#> 7 7 360
#> 8 8 860
#> 9 9 300
#> 10 10 240
Let's say I want to add columns for the difference in sales since yesterday, the day before, and the day before that.
I could do so with three lines in mutate()
.
df1 %>% mutate(
# this line creates "lag1"
# the lag function looks 'n' rows above, so I run it
# one time for each row
sales_chg_d1 = sales - lag(sales, n = 1),
sales_chg_d2 = sales - lag(sales, n = 2),
sales_chg_d3 = sales - lag(sales, n = 3))
#> # A tibble: 10 x 5
#> day sales sales_chg_d1 sales_chg_d2 sales_chg_d3
#> <int> <int> <int> <int> <int>
#> 1 1 260 NA NA NA
#> 2 2 230 -30 NA NA
#> 3 3 110 -120 -150 NA
#> 4 4 420 310 190 160
#> 5 5 420 0 310 190
#> 6 6 380 -40 -40 270
#> 7 7 360 -20 -60 -60
#> 8 8 860 500 480 440
#> 9 9 300 -560 -60 -80
#> 10 10 240 -60 -620 -120
Created on 2020-07-07 by the reprex package (v0.3.0)
Is there any way to run this on three days at once, in one line of mutate? That way, I could run it easily for all 10 days, or something like that. I tried playing around with across()
, but that doesn't seem to work.
Thank you!