使用变量名对多个列进行分组

使用变量名对多个列进行分组

本文介绍了dplyr-使用变量名对多个列进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与R Shiny合作进行一些探索性数据分析。我有两个复选框输入,它们仅包含用户选择的选项。第一个复选框输入仅包含分类变量。第二个复选框仅包含数字变量。接下来,在这两个选择上应用 groupby

I am working with R Shiny for some exploratory data analysis. I have two checkbox inputs that contain only the user-selected options. The first checkbox input contains only the categorical variables; the second checkbox contains only numeric variables. Next, I apply a groupby on these two selections:

var1 <- input$variable1      # Checkbox with categorical variables
var2 <- input$variable2      # Checkbox with numerical variables

v$data <- dataset %>%
  group_by_(var1) %>%
  summarize_(Sum = interp(~sum(x), x = as.name(var2))) %>%
  arrange(desc(Sum))

仅选择一个类别变量时,此 groupby 起作用完美。当多个分类变量被选择,这 GROUPBY 返回与列名的数组。如何将此列名称数组传递给 dplyr groupby

When only one categorical variable is selected, this groupby works perfectly. When multiple categorical variables are chosen, this groupby returns an array with column names. How do I pass this array of column names to dplyr's groupby?

推荐答案

如果有向量变量名,则应将它们传递给 .dots = group_by _ 的参数。例如:

If you have a vector of variable names, you should pass them to the .dots= parameter of group_by_. For example:

mtcars %>%
   group_by_(.dots=c("mpg","hp","wt")) %>%
   summarize(x=mean(gear))

这篇关于dplyr-使用变量名对多个列进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:25