我试图编写一个执行split-apply-combine的函数,其中split split(s)是参数,并且 - 重要的是 - 可以接受空分割。例如,在数据子集或整个数据集上运行统计信息。
somedata=expand.grid(a=1:3,b=1:3)
somefun=function(df_in,grpvars=NULL){
df_in %>% group_by_(.dots=grpvars) %>% nest() %>%
mutate(X2.Resid=map(data,~with(.x,chisq.test(b)$residuals))) %>%
unnest(data,X2.Resid) %>% return()
}
somefun(somedata,"a") # This works
somefun(somedata) # This fails
null条件失败,因为nest()似乎需要一个变量来嵌套,而不是将整个df嵌套到1x1 data.frame中。我可以通过以下方式解决这个问题:
somefun2=function(df_in,grpvars="Dummy"){
df_in$Dummy=1
df_in %>% group_by_(.dots=grpvars) %>% nest() %>%
mutate(X2.Resid=map(data,~with(.x,chisq.test(b)$residuals))) %>%
unnest(data,X2.Resid) %>%
select(-Dummy) %>% return()
}
somefun2(somedata) # This works
但是,我想知道是否有更优雅的方法来解决这个问题,而不需要虚拟变量?
答案 0 :(得分:4)
everything()
:
somefun3 <- function(df_in, grpvars = NULL) {
df_in %>%
group_by_(.dots = grpvars) %>%
nest(everything()) %>%
mutate(X2.Resid = map(data, ~with(.x, chisq.test(b)$residuals))) %>%
unnest()
}
somefun3(somedata, "a")
somefun3(somedata)
两者都有效。