Zhixue Su
Zhixue Su

Reputation: 41

keep original variable name when do grouping summary using summarize() in R function

I am trying to write a function in R to summarize a table. The following is an example function and I am using the Iris data as a test.

test_func <- function(data, by_var_nm) {
  by_var_nm <- deparse(substitute(by_var_nm))

  tbl_test_sum <- data %>% 
    group_by(data[[by_var_nm]]) %>% 
    summarise(
      count = n()
    )
  tbl_test_sum
}

test_func(iris, Species)

As you could see, the output in the following section has a problem, in which the first variable in the table is called "data[[by_var_nm]]" instead of "Species". Is there any way that I could maintain the original variable name during the summarizing process?

# A tibble: 3 x 2
  `data[[by_var_nm]]` count
  <fct>               <int>
1 setosa                 50
2 versicolor             50
3 virginica              50

Thank you.

Thank you all for very helpful answer. I tried the solutions and it seems snoram's answer solved my initial problem quite well. However, after I combined everything together, I couldn't get the last bit of the plot working properly. The idea is that I want to plot the percentage distribution on the "var_nm" and group them by "by_var_nm". The problem I got is that the bar graph and also the percentage for the data label are not lined up properly.

test_func <- function(data, var_nm, by_var_nm) {
  var_nm <- deparse(substitute(var_nm))
  by_var_nm <- deparse(substitute(by_var_nm))

  tbl_test_sum <- as.data.frame(table(data[[by_var_nm]], data[[var_nm]]))
  names(tbl_test_sum) <- c(by_var_nm, var_nm, "count")

  # tbl_test_sum

  tbl_test_total <- as.data.frame(table(data[[by_var_nm]]))
  names(tbl_test_total) <- c(by_var_nm, "total")

  # tbl_test_total

  tbl_test_pctg <- full_join(tbl_test_sum, tbl_test_total, by = by_var_nm) %>%
    mutate(
      percentage = count / total
    )

  # tbl_test_pctg

  ggplot(data=tbl_test_pctg, aes(x = tbl_test_pctg[[var_nm]], y = percentage, fill = tbl_test_pctg[[var_nm]])) +
    geom_bar(stat="identity") +
    geom_text(aes(label = scales::percent(percentage))) +
    facet_grid(tbl_test_pctg[[by_var_nm]]~.) +
    coord_flip()
}

test_func(mtcars, cyl, am)

enter image description here

Upvotes: 3

Views: 261

Answers (3)

akuiper
akuiper

Reputation: 215077

You can use rlang's Quotation syntax, which is designed for this use case; Also read the examples here:

library(rlang); library(dplyr)

test_func <- function(data, by_var_nm) {
    by_var_nm <- enquo(by_var_nm)

    tbl_test_sum <- data %>% 
        group_by(!!by_var_nm) %>% 
        summarise(
            count = n()
        )
    tbl_test_sum
}

test_func(iris, Species)

# A tibble: 3 x 2
#  Species    count
#  <fct>      <int>
#1 setosa        50
#2 versicolor    50
#3 virginica     50

Upvotes: 1

s_baldur
s_baldur

Reputation: 33548

Suggesting similar solution as Alexandre but breaking dplyr dependency at the same time. If you are planning on keeping this function I think unnecessary dependencies is not a good idea.

test_func <- function(data, by_var_nm) {
  by_var_nm <- deparse(substitute(by_var_nm))
  tbl_test_sum <- as.data.frame(table(data[[by_var_nm]]))
  names(tbl_test_sum) <- c(by_var_nm, "count")
  tbl_test_sum
}

Speed:

> microbenchmark::microbenchmark(test_func_Alex(iris, Species), test_func_snoram(iris, Species), unit = "relative")
Unit: relative
                            expr      min       lq     mean   median       uq      max neval cld
   test_func_Alex(iris, Species) 6.910679 6.834064 5.827796 5.622154 5.480321 4.009469   100   b
 test_func_snoram(iris, Species) 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000   100  a

Upvotes: 1

Alexandre georges
Alexandre georges

Reputation: 667

I don't know why this is happening but you can use this trick to get back the name :

test_func <- function(data, by_var_nm) {
  by_var_nm <- deparse(substitute(by_var_nm))

  tbl_test_sum <- data %>% 
    group_by(data[[by_var_nm]]) %>% 
    summarise(
      count = n()
    )
  names(tbl_test_sum)[grep("by_var_nm",names(tbl_test_sum))] <- by_var_nm
  tbl_test_sum
}

test_func(iris, Species)

You can also use the index names(tbl_test_sum)[1] assuming the group_by() is creating the first column on this variable.

Hope this will help you

Upvotes: 0

Related Questions