Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

function

r

dplyr

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

like image 963
Zhixue Su Avatar asked Sep 13 '26 19:09

Zhixue Su


2 Answers

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
like image 51
sindri_baldur Avatar answered Sep 16 '26 09:09

sindri_baldur


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
like image 33
Psidom Avatar answered Sep 16 '26 10:09

Psidom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!