我想将可管理的函数(magrittr / dplyr)总结为更短的函数 - " blocks" (希望有更多可读代码)。例如:
library(dplyr)
d <- tbl_df(data.frame(A = rep(LETTERS[2:5], each = 5),
M = rep(letters[1:2], times = 10),
X = round(rnorm(20, 10, 2), 1)))
# I want to replace this
# ----------------------
d %>%
group_by(A) %>%
summarise(X = mean(X)) -> d_test_1
# with this
# ---------
my_mean <- function(d, by_var, x) {
expr <- substitute(by_var) # group variable, seems ok
expr_2 <- substitute(expression(x = mean(x))) # calculate mean
print(deparse(expr_2))
# problem: x = mean(x) is only substituted to x = mean(X) .. 1 capital x, should be 2
expr_3 <- parse(text = paste(deparse(substitute(x)), "=mean(",
deparse(substitute(x)), ")"))
print(deparse(expr_3))
# expr_3 does not work either
d %>%
group_by(eval(expr)) %>%
#summarise(X = mean(X)) -> d # uses right group variable
summarise(eval(expr_3)) -> d # uses wrong group variable <> side-effect of "expr"?
invisible(d)
}
# this is the short version I am after
d %>%
my_mean(A, X) -> d_test_2
d_test_1
d_test_2
Thx&amp;亲切的问候
答案 0 :(得分:1)
也许别人不知道在哪里看:
const std::vector<std::map<Id, std::vector<Data>>> *const DataSets
{
&object.data1,
&object.data2,
&object.data3,
&object.data4
};