- library(tidyverse)
- dim_names <- c("x1", "x2") # 数据维度的名称
- idx_names <- c("y1", "y2") # 数据指标的名称
- fun_names <- c("mean", "max") # 要应用的函数的名称
- # dim_names的循环,用lapply来实现
- # idx_nemas的循环,用summarise_at实现
- lapply(dim_names, function(x) {
- data %>%
- group_by(get(x)) %>%
- summarise_at(vars(idx_names),
- sapply(fun_names, get)) %>%
- set_names(c("dim_value", colnames(.)[-1])) %>%
- mutate(dim_name = x)
- }) %>%
- bind_rows() %>%
- gather(idx_name_fun, value, c(-1, -ncol(.))) %>%
- separate(idx_name_fun, c("idx_name", "fun")) %>%
- select(dim_name, dim_value, everything()) %>%
- spread(fun, value) %>%
- distinct()
麻烦你反馈一下代码执行一次计算需要多长时间?


雷达卡




京公网安备 11010802022788号







