将函数中数据帧的列名传递给R中的dplyr函数count()?
如何将函数将函数中数据帧的列名传递给R中的dplyr函数count()?,r,dplyr,R,Dplyr,如何将函数foo的参数ColName交给R函数countColName是数据帧中列的名称 library(scales) library(dplyr) foo <- function(df, ColName, YearCol){ percentData <- df %>% group_by(format(as.Date(df[,YearCol]),"%Y")) %>% count(ColName) %>% # do
foo
的参数ColName
交给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)))
}
库(比例)
图书馆(dplyr)
富%
count(ColName)%>%#不这样工作,df[,ColName]也不工作
变异(比率=比例::百分比(n/总和(n)))
}
您可以使用select
的.dots
参数来选择您感兴趣的列
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
}
foo%
分组依据(格式(截止日期(.dots2),%Y”)%>%
计数(.dots1)%>%
变异(比率=比例::百分比(n/总和(n)))
百分比数据
}