It turns out I’m too late posting this to get credit, but I’ll post it anyway because the work is done. Note to self: remember to check due time as well as due date.
The assignment was to find various calculations for two vectors then compare them. My code:
x <- c(10, 2, 3, 2, 4, 2, 5) y <- c(20, 12, 13, 12, 14, 12, 15) # Took function to find the mode from: # https://stackoverflow.com/a/8189441 # The mode() function reports a values type (like "numeric") get_mode <- function(x) { ux <- unique(x) ux[which.max(tabulate(match(x, ux)))] } get_cv <- function(x) { sd(x) / mean(x) * 100 } cat("For X, mean:", mean(x), "median:", median(x), "mode:", get_mode(x), "\n") cat("For Y, mean:", mean(y), "median:", median(y), "mode:", get_mode(y), "\n") cat("--\n") # Assuming that the question was for quantile, given that was the function # described - otherwise I'd use the IQR() function to find interquartile range cat("For X, range:", range(x), "quantile:", quantile(x), "variance:", get_cv(x), "standard deviation:", sd(x), "\n") cat("For Y, range:", range(y), "quantile:", quantile(y), "variance:", get_cv(y), "standard deviation:", sd(y), "\n")
The result:
For X, mean: 4 median: 3 mode: 2 For Y, mean: 14 median: 13 mode: 12 -- For X, range: 2 10 quantile: 2 2 3 4.5 10 variance: 72.16878 standard deviation: 2.886751 For Y, range: 12 20 quantile: 12 12 13 14.5 20 variance: 20.61965 standard deviation: 2.886751
The most interesting result when comparing the two datasets was that the standard deviance was identical for both sets, despite the ranges and variances being very different.
The assignment was straightforward, mostly because of previous courses using R – I’ve used these functions before, apart from variance and the custom mode function I picked up from the Internet.