r - 将函数中数据框的列名移交给 R 中的 dplyr 函数 count()?
问题描述
如何将ColName
函数的参数移交foo
给 R 函数count
?ColName
是数据框中列的名称。
library(scales)
library(dplyr)
foo <- function(df, ColName, YearCol){
percentData <- df %>%
group_by(format(as.Date(df[,YearCol]),"%Y")) %>%
count(ColName) %>% # does not work like this, also df[,ColName] does not work
mutate(ratio=scales::percent(n/sum(n)))
}
解决方案
您可以使用.dots
参数select
来选择您感兴趣的列。
foo <- function(df, ColName, YearCol){
percentData <- df %>%
select(.dots = c(ColName, YearCol)) %>%
group_by(format(as.Date(.dots2), "%Y")) %>%
count(.dots1) %>%
mutate(ratio=scales::percent(n/sum(n)))
percentData
}
推荐阅读
- python - Google Cloud Stackdriver 忽略 Python 日志中的格式
- .net-core - 具有本机库依赖的 .NET Core 项目
- r - windows下R的glmnet
- javascript - 在 Java Script 中从数组中添加数字时,如何避免 NaN?
- hyperledger-fabric - 如何删除 Hyperledger Fabric 中的所有交易?
- apache-spark - 如何为foreachBatch的batchId设置起点?
- vue.js - 具有唯一值的 Laravel VUEJS 动态行
- matlab - 在 GPU 上运行 MATLAB 图像处理脚本
- css - 在 React 中再次触发动画时重置动画(使用钩子) - 滑动文本
- android - 如何按项目名称而不是按位置设置 Spinner 位置?