fixing decimal values in function

I have a data frame below

db <- data.frame(aa = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50),
                    a1 = c(31,60,23,40,32,44,39,50,29,22,57,55,39,54,48,40,60,60,41,29,31,29,49,33,41,59,35,50,28,36,34,60,39,40,43,25,38,51,48,31,49,28,48,59,58,22,57,40,36,51))

now i am trying to create a function get output in decimal values and without decimal values
so my function will be look like

one_var(dataset=db,var=a1,Name_of_variable="years",decimal=True)

one_var<-function(dataset,var,Name_of_variable,decimal) {

var <- rlang::parse_expr(var)
summ_tab1<- dataset %>% filter(!is.na(!!var)) %>%   summarise(
  q25 = round(quantile(!! var,  type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2],ifelse(decimal==TRUE,digits = 1,digits = 0)),
  Median =round(quantile(!! var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3],ifelse(decimal==TRUE,digits = 1,digits = 0)),
  Average = round( mean(!! var, na.rm=TRUE),ifelse(decimal==TRUE,digits = 1,digits = 0)),
  q75 = round(quantile(!! var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[4],ifelse(decimal==TRUE,digits = 1,digits = 0)) ,
  N = sum(!is.na(!!var)))
summ_tab1
}

so if decimal is true it should show all values with one decimal ,if decimal is false then it should show all values without decimal.

but i am getting error for creating this.

your function called one_var is missing a function body i.e.

myfunc <- function(myparm)
{
function body 
} # final bracket marks end of the function

myfunc(myparm=1) #use function

it also seems that you will be ignoring dataset param value and forcing it to be db each time, this is probably not desirable

I havent run your new code but this will make decimal to equal true. To test rather than set , use double equals
decimal == TRUE

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

I have just updated the function, actually that was i was playing to create function

Its not appropriate to use the vectorised version of if else, rather a single value of decimal decides for your whole code the number of digits to use. so this is preferred :

library(tidyverse)
db <- data.frame(aa = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50),
                 a1 = c(31,60,23,40,32,44,39,50,29,22,57,55,39,54,48,40,60,60,41,29,31,29,49,33,41,59,35,50,28,36,34,60,39,40,43,25,38,51,48,31,49,28,48,59,58,22,57,40,36,51))

one_var<-function(dataset,var,decimal) {
  
  var <- rlang::parse_expr(var)
    numdig <- if (decimal == TRUE) {1} else {0}

  summ_tab1<- dataset %>% filter(!is.na(!!var)) %>%   summarise(
    q25 = round(quantile(!! var,  type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2],digits=numdig),
    Median =round(quantile(!! var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3],digits= numdig),
    Average = round( mean(!! var, na.rm=TRUE),digits=numdig),
    q75 = round(quantile(!! var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[4],digits= numdig),
    N = sum(!is.na(!!var)))
  summ_tab1
}

one_var(db,"aa",FALSE)
one_var(db,"aa",TRUE)

I have done but still getting error

Error in ifelse(decimal == TRUE, digits = 1, digits = 0) :
unused arguments (digits = 1, digits = 0)

Thanks nirgrahamuk for helping me always, i have also resolved my decimal values problem like previously 20.0 was showing as 20 but not its 20.0

numdig <- if (decimal == TRUE) {1} else {0}
var <- rlang::parse_expr(var)
summ_tab1<- dataset %>% filter(!is.na(!!var)) %>%   summarise(
  q25 = format(round(quantile(!! var,  type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2],digits = numdig),nsmall = 1),
  Median = format(round(quantile(!! var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3],digits = numdig),nsmall = 1),
  Average = format(round( mean(!! var, na.rm=TRUE),digits = numdig),nsmall = 1),
  q75 = format(round(quantile(!! var, type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[4],digits = numdig) ,nsmall = 1),
  N = sum(!is.na(!!var)))