Home > Enterprise >  change the name of label in summary table dynamically
change the name of label in summary table dynamically

Time:11-24

I am trying to make my function dynamic like i want to show q25 dynamically. like i want to declare percentile25 dynamically i rmarkdown if i want to otherwise by default wanted to keep.

i have tried set option, get option but getting error everytime.

df <- data.frame(Name = c("asdf","kjhgf","cvbnm","rtyui","cvbnm","jhfd","cvbnm","sdfghj","cvbnm","dfghj","cvbnm"),
                 sale=c(27,28,27,16,14,25,14,14,19,18,28),
                 city=c("CA","TX","MN","NY","TX","MT","HU","KL","TX","SA","TX"),
                 Dept = c("HH","MM","NN","MM","AA","VV","MM","HU","JJ","MM","ZZ"))

percentile25 <- "25th Perc"

t1<- function(dataset,var,name,options(percentile25 = "25th percentile"),..)
  {
var <- rlang::parse_expr(var)
tabl1 <- dataset %>% dplyr::filter(!is.na(!!var)) %>% summarise(
    q25 = quantile(!! var,  type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2],
    N = sum(!is.na(!!var)))
  summ_tab<-tabl1 %>%  
    mutate(" "= !!name,
           q25 = q25) 
  summ_tab <- summ_tab %>%  dplyr::rename(
    !!p25 := q25)
  summ_tab <- summ_tab %>% select(" ",everything(),N)
  summ_tab
}

t1(data = df,var = "sale",name = "listd")

for Mediana

t1<- function(dataset,var,name,p25 = getOption("percentile25", default = "25th percentile")
              med=getOption(Med_n ,default ="Median")){
var <- rlang::parse_expr(var)
tabl1 <- dataset %>% dplyr::filter(!is.na(!!var)) %>% summarise(
    q25 = quantile(!! var,  type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2],
    med = quantile(!! var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3],
    N = sum(!is.na(!!var)))
  summ_tab<-tabl1 %>%  
    mutate(" "= !!name,
           q25 = q25,
           Median = Median) 
  summ_tab <- summ_tab %>%  dplyr::rename(
    !!p25 := q25,
    !!med:=Median)
  summ_tab <- summ_tab %>% select(" ",everything(),N)
  summ_tab
}

CodePudding user response:

You have misunderstood my comment on your former question:

Stick with the definition of the function in your former post:

library(dplyr)

t1 <- function(dataset, var, name, p25 = getOption("percentile25", default = "25th percentile")) {
  var <- rlang::parse_expr(var)

  tabl1 <- dataset %>%
    filter(!is.na(!!var)) %>%
    summarise(
      q25 = quantile(!!var, type = 6, probs = seq(0, 1, 0.25), na.rm = TRUE)[2],
      N = sum(!is.na(!!var))
    )
  summ_tab <- tabl1 %>%
    mutate(
      " " = !!name,
      q25 = q25
    )
  summ_tab <- summ_tab %>% dplyr::rename(
    !!p25 := q25
  )
  summ_tab <- summ_tab %>% select(" ", everything(), N)
  summ_tab
}

Then option one to pass your desired label to the function would be to pass the name directly to the p25 argument:

t1(data = df, var = "sale", name = "listd", p25 = "25th Percentilen")
#>         25th Percentilen  N
#> 1 listd               14 11

Second option would be to set your desired label via options outside of your function. In that case getOption will automatically pick the label:

options(percentile25 = "25th Percentilen")

t1(data = df, var = "sale", name = "listd")
#>         25th Percentilen  N
#> 1 listd               14 11

UPDATE And here is the updated function which now includes the median. Additionally I made some slight adjustments, e.g. if you want to compute just one quantile you could do so by using e.g. probs = .25. Also I collapsed the rename and select into one step:

t1 <- function(dataset, var, name,
               p25 = getOption("percentile25", default = "25th percentile"),
               med = getOption("Med_n", default = "Median")) {
  var <- rlang::parse_expr(var)

  tabl1 <- dataset %>%
    filter(!is.na(!!var)) %>%
    summarise(
      q25 = quantile(!!var, type = 6, probs = .25, na.rm = TRUE),
      med = quantile(!!var, type = 6, probs = .5, na.rm = TRUE),
      N = sum(!is.na(!!var))
    )
  summ_tab <- tabl1 %>%
    mutate(
      " " = !!name
    ) %>%
    select(" ", !!p25 := q25, !!med := med, N)
  summ_tab
}

t1(
  data = df, var = "sale", name = "listd",
  p25 = "25th Percentilen", med = "Mediana"
)
#>         25th Percentilen Mediana  N
#> 1 listd               14      19 11

options(
  percentile25 = "25th Percentilen",
  Med_n = "Mediana"
)

t1(data = df, var = "sale", name = "listd")
#>         25th Percentilen Mediana  N
#> 1 listd               14      19 11
  •  Tags:  
  • r
  • Related