r - dplyr: How to use select and filter inside functions; (...) not working for arguments -
i'm trying build functions creating standard tables questionnaire, using dplyr data manipulation. question helpful group_by function, passing arguments (in case, name of variable want use make table) (...)
, seems break down when trying pass same arguments other dplyr commands, 'select' , 'filter'. error message '...' used in incorrect context'
.
does have ideas on this? thank you
for sake of completeness (and other hints - i'm new writing functions), here code use:
mytable <- function(x, ...) { df <- x %>% group_by(var1, ...) %>% filter(!is.na(...) & ... != '') %>% # remove missing values: not working! summarise(value = n()) %>% group_by(var1) %>% mutate(tot = sum(value)) %>% group_by(var1, ...) %>% summarise(num = sum(value), total = sum(tot), proportion = num/total*100) %>% select(var1, ..., proportion) # select desired columns: not working! tab <- dcast(df, var1 ~ ..., value.var = 'proportion') tab[is.na(tab)] <- 0 print(tab) }
Comments
Post a Comment