0
votes

I am trying to write a function to create summary table. The variables i am interested to summarise might change, therefore, I would like to have it in a function. I followed the example from the dpylr vignette about NSE, but it is not working for me for some reason. Here is the function:

print(agegroup) # this is a string
table_summary <- function (data, group_by1){

  quo_group_by1 = quo(group_by1)
  print(quo_group_by1)

  data %>%
  dplyr::group_by(!! quo_group_by1) %>%
  dplyr::summarise(N = n()) %>%
  dplyr::mutate(pct = paste0((round(N/sum(N)*100, 2))," %"))

}

table_summary(clientData, agegroup)

and i get the following error :

[1] "ag5"
<quosure>
expr: ^group_by1
env:  0x7faaec29e030
Error: Column `group_by1` is unknown

How can i fix this?

1

1 Answers

0
votes

You should use curly-curly ({{}}) which avoids quo & !!. Also you can use count which is a shortcut for group_by + summarise.

table_summary <- function (data, group_by1){

   data %>%
    dplyr::count({{group_by1}}) %>%
    dplyr::mutate(pct = paste0((round(N/sum(N)*100, 2))," %"))
}

table_summary(clientData, agegroup)

It seems agegroup is a string. To continue with OP's approach we need to convert it to symbol (sym) and evaluate it (!!)

table_summary <- function (data, group_by1){

  data %>%
    dplyr::group_by(!!sym(group_by1)) %>%
    dplyr::summarise(N = n()) %>%
    dplyr::mutate(pct = paste0((round(N/sum(N)*100, 2))," %"))

}