\[ CV = \frac {\sigma}{\mu} \]
udf_cv <- function(x, na.rm = F) {
sd(x, na.rm = na.rm) / mean(x, na.rm = na.rm)
}
pg_body_cv <- udf_cv(pg$body_mass_g, na.rm = T)
pg_body_cv
## [1] 0.1908618
pg %>%
mutate(year = as.factor(year)) %>%
summarize(across(where(is.numeric), ~ udf_cv(., na.rm = T)))
## # A tibble: 1 x 4
## bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
## <dbl> <dbl> <dbl> <dbl>
## 1 0.124 0.115 0.0700 0.191
library(goeveg)
cv(pg$body_mass_g, na.rm = T)
## [1] 0.1908618
pg %>%
mutate(year = as.factor(year)) %>%
summarize(across(where(is.numeric), ~ cv(., na.rm = T)))
## # A tibble: 1 x 4
## bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
## <dbl> <dbl> <dbl> <dbl>
## 1 0.124 0.115 0.0700 0.191
samp1_1 <- c(1:10); sd(samp1_1)
## [1] 3.02765
samp1_2 <- c(1:10)*10; sd(samp1_2)
## [1] 30.2765
cv(samp1_1); cv(samp1_2)
## [1] 0.5504819
## [1] 0.5504819
samp2_1 <- rnorm(10, 100, 100); sd(samp2_1)
## [1] 110.2341
samp2_2 <- rnorm(10, 1000, 100); sd(samp2_2)
## [1] 133.8232
cv(samp2_1); cv(samp2_2)
## [1] 0.8183151
## [1] 0.1412784