I'm trying to write an R function to produce a frequency table so I can standardise formatting etc without typing it out repeatedly. The only problem is that I can't get it to evaluate a grouping variable correctly.
Here is some code to get a mini dataset to reproduce the problem:
library(tidyverse)
id <- sample(1:500, 5)
factors <- sample(1:3, 5, replace = TRUE)
data <- data.frame(id, factors)
freqTable <- function(x, field){
Table <- x %>%
group_by(field) %>%
summarise(N = n(), Percent = n()/NROW(x)*100) %>%
mutate(C.Percent = cumsum(Percent))
return(Table)
}
freqTable(data, "factors")
Which results in:
Error in resolve_vars(new_groups, tbl_vars(.data)) : unknown variable to group by : field Called from: resolve_vars(new_groups, tbl_vars(.data))
I've also tried:
freqTable <- function(x, field){
Table <- x %>%
group_by(paste(field)) %>%
summarise(N = n(), Percent = n()/NROW(x)*100) %>%
mutate(C.Percent = cumsum(Percent))
return(Table)
}
Which works a little better (in that it doesn't error), but still doesn't actually group the factors correctly, outputting this:
# A tibble: 1 × 4
`paste(field)` N Percent C.Percent
<chr> <int> <dbl> <dbl>
1 factors 5 100 100
Where it just tells me the number of cases in that column. Does anyone know where I'm going wrong here?