0

I'm trying to create my own function to compute Coefficient of Variation given any variable (column) within a dataframe. So far I've got the below code

coeffvar <- function(variable)
  { 
  abs((SD(variable) / mean(variable))*100)
  }

Which works for single vectors, but when the argument is a dataframe, I get the following error:

Warning message:
In mean.default(variable) :
  argument is not numeric or logical: returning NA

and all outputs are NA

  • 1
    Can you post sample data? Please edit the question with the output of `dput(df)`. Or, if it is too big with the output of `dput(head(df, 6))`. Try calling the function with `coeffvar(df$variable)`. – Rui Barradas Feb 03 '23 at 15:15

2 Answers2

1

You can wrap your function in a loop that iterates over colnames(df) or you could try implementations similar to the ones shared here: Apply function to each column in a data frame observing each columns existing data type

coeffvar = function(variable)
  { 
  abs((sd(variable) / mean(variable))*100)
  }

coeffvarS = function(df){
  sapply(df, function(x) coeffvar(x) )
}


coeffvarS(mtcars)

#Output:
    mpg       cyl      disp        hp      drat        wt      qsec        vs 
 29.99881  28.86338  53.71779  46.74077  14.86638  30.41285  10.01159 115.20369 
       am      gear      carb 
122.82853  20.00825  57.42933 

If you wanted one function for both vectors and data.frames you could add an if statement that checks the data type of the function input.

Claudio Paladini
  • 1,000
  • 1
  • 10
  • 20
  • 1
    this is a wonderful workaround! i used your example code and that worked perfectly. i will take your suggestion on an if-statement idea to combine the two functions. thank you! – Solve_for_T Feb 03 '23 at 16:11
0

Please check the below code with map_if working only on numeric columns

coeffvar <- function(variable){
do.call(cbind, map_if(variable, is.numeric, ~ abs(sd(.x)/mean(.x))*100)) %>% 
  as_tibble() %>% unnest(cols = everything())
}

coeffvar(mtcars)

Created on 2023-02-03 with reprex v2.0.2

# A tibble: 1 × 11
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  30.0  28.9  53.7  46.7  14.9  30.4  10.0  115.  123.  20.0  57.4
jkatam
  • 2,691
  • 1
  • 4
  • 12