dplyr-通过for循环r分组 [英] dplyr- group by in a for loop r

查看:112
本文介绍了dplyr-通过for循环r分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在for循环中使用group by.我希望通过循环浏览每一列,然后执行摘要操作.我尝试在groupby中使用colnames(df [i]),但是因为colnames带有引号,所以此方法不起作用.

I am trying to use group by in a for loop. I would like the gourp by to cycle through each column and then I can perform a summarise action. I tried to used colnames(df[i]) within the groupby but because colnames comes back with quotation marks this method does not work.

有什么建议吗?

推荐答案

如果您还没有设置for循环,最简单的方法可能是使用dplyr :: summarise_all或dplyr :: sumarise_at-取决于您的需求.

If you aren't dead set on using a for loop, the easiest way may be to use dplyr::summarise_all or dplyr::sumarise_at - depending on your needs.

df <- tibble(
  var1 = c(rep("a", 5), rep("b", 5)),
  var2 = rnorm(10),
  var3 = rnorm(10)
)

df %>% 
  group_by(var1) %>% 
  summarise_all(funs(mean = mean))

# A tibble: 2 x 3
   var1  var2_mean  var3_mean
  <chr>      <dbl>      <dbl>
1     a -0.2715518 -0.6146812
2     b  0.1502118 -0.2061952

更新:

对不起,我以前误读了这个问题.您想遍历"group_by"变量,而不是响应变量.

Sorry, I previously misread the question. You want to loop over the "group_by" variables - not the response variables.

您可以使用整洁的评估.这是一个示例:

You can do that with tidy eval. Here is an example:

df <- tibble(
  var1 = c(rep("a", 5), rep("b", 5)),
  var2 = c(rep("c", 3), rep("d", 7)),
  var3 = rnorm(10)
)

groups <- c(quo(var1), quo(var2))  # Create a quoture

for (i in seq_along(groups)) {
  df %>% 
    group_by(!!groups[[i]]) %>% # Unquote with !!
    summarise(mean = mean(var3)) %>% 
    print()
}

# A tibble: 2 x 2
   var1       mean
  <chr>      <dbl>
1     a -0.3451196
2     b  0.4117763
# A tibble: 2 x 2
   var2       mean
  <chr>      <dbl>
1     c -0.2618434
2     d  0.1598305

这篇关于dplyr-通过for循环r分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆