r:dplyr函数用于制作具有多个组的长格式

时间:2017-10-16 18:37:04

标签: r dplyr tidyr

我试图将此代码转换为函数:

mtcars %>% group_by(gear) %>% select(hp, disp) %>% 
summarise_all(funs(n=sum(!is.na(.)), mean=mean(.,na.rm=T))) %>% 
gather(variable, value, -gear) %>% 
arrange(gear, sub('_.*', '', variable), sub('.*_', '', variable)) %>%
separate(variable, into = c('var', 'metric'), '_')

它的作用是创建一个我可以轻松导入Excel并创建数据框的数据框。我尝试了以下方法,但它似乎无法运作:

mean_func <- function(vars,groups) {
  results <- test %>% group_by_at(vars(one_of(groups))) %>% 
    summarise_at(vars(starts_with(vars)), funs(n=sum(!is.na(.)), mean=mean(.,na.rm=T))) %>% 
    gather_(variable, value, -groups) %>% 
    arrange_(groups) %>%
    separate_(variable, into = c('var', 'metric'), '_'); View(results)
}

似乎问题出现在gather声明的某处,但我不确定这里有什么问题。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

使用一点tidy清除来更改分组变量并使用group_by_进行编程。

library(tidyverse)

mean_func <- function(vars, groups) {
    groups = enquo(groups)

    vars %>% 
        group_by_(groups) %>% 
        dplyr::select(hp, disp) %>% 
        summarise_all(funs(n=sum(!is.na(.)), mean=mean(.,na.rm=T))) %>%
        gather(variable, value, -!!groups) %>%
        arrange(!!groups, sub('_.*', '', variable), sub('.*_', '', variable)) %>%
        separate(variable, into = c('var', 'metric'), '_')
}
mean_func(mtcars, gear)