Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将函数中数据帧的列名传递给R中的dplyr函数count()?_R_Dplyr - Fatal编程技术网

将函数中数据帧的列名传递给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)))
百分比数据
}