假设我下面有一些随时间和位置变化的数据
library(tidyverse)
dts <- seq(
ymd_hms('2023-01-01 00:00:00'),
ymd_hms('2025-01-01 00:00:00'),
by = '1 min'
)
locs <- tribble(
~'continent', ~'country', ~'city',
'NA', 'CA', 'Toronto',
'NA', 'US', 'Los Angeles',
'EU', 'UK', 'London',
'EU', 'FR', 'Paris'
)
d <- crossing(locs, dates=dts) %>%
mutate(
second = second(dates),
min = minute(dates),
hour = hour(dates),
yday = yday(dates),
month = month(dates, label=T),
y = runif(n())
)
创建于2024-02-18年第reprex v2.0.2页
我的目标是创建一个名为rollup
的函数,用户可以按时间维度和位置维度进行分组. 理想情况下,用户可以传递参数,如
rollup(d, time_dims = c(year, month), loc_dims = c(country))
而输出结果将是
d %>%
group_by(all the variables in loc dims and time dims) %>%
summarise(y = mean(y))
如果参数是字符向量,这就很简单了
rollup <- function(.data, time_dims, loc_dims) {
.data %>%
group_by_at(c(time_dims, loc_dims)) %>%
summarise(y = mean(y))
}
rollup(d, time_dims = c('min', 'hour'), loc_dims = c('country'))
`summarise()` has grouped output by 'min', 'hour'. You can override using the
`.groups` argument.
# A tibble: 5,760 × 4
# Groups: min, hour [1,440]
min hour country y
<int> <int> <chr> <dbl>
1 0 0 CA 0.496
2 0 0 FR 0.514
3 0 0 UK 0.500
4 0 0 US 0.504
5 0 1 CA 0.511
6 0 1 FR 0.509
7 0 1 UK 0.489
8 0 1 US 0.505
9 0 2 CA 0.486
10 0 2 FR 0.484
# ℹ 5,750 more rows
# ℹ Use `print(n = ...)` to see more rows
如果我想传递一个符号数组,比如pivot_longer
可以接受,该怎么办?如何修改rollup
以接受time_dims
和loc_dims
的符号数组?