0

I want a function to calculate the mean and variance of a vector at the same time (with a vector output) such that if I only want the value of the mean or only that of the variance I will only subset it with a $ sign.

x <- function(n, a, b, mean, sd){
  meann = mean(truncnorm::rtruncnorm(n, a, b, mean, sd))
  std = sd(truncnorm::rtruncnorm(n, a, b, mean, sd))
  return(c(meann, std))
}
x(n=100, a = -1, b = 1, mean = 0, sd = 1)

If I want mean I will write x$meann and if I need standard deviation I will write x$std

Daniel James
  • 1,381
  • 1
  • 10
  • 28
  • 2
    `return(list(meann = meann, std = std))` will give you a named output. – Paul Stafford Allen Jan 30 '23 at 14:30
  • 1
    Note that `meann = ` and `std = ` generate 2 different sample populations. Better to initially generate a single sample population and take the mean and std from that. You may also consider setting a seed in the function if it would support your output objective. – SteveM Jan 30 '23 at 14:52

0 Answers0