[英]Handing over the column name of a data frame in a function to the dplyr function count() in R?
如何將我的 function foo
的參數ColName
移交給 R function count
? ColName
是 dataframe 中列的名稱。
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)))
}
您可以使用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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.