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
CodePudding user response:
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.
CodePudding user response:
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