Function is as below:
calculateMeanVar <- function(x) {
x_mean <- mean(x)
x_var <- var(x)
return(data.frame(xmean = x_mean, xvar = x_var))
}
Now, test you function on the petal length of iris data set (Built in data set in R):
- Include standard error in the function
- Include the standard error in the output of the function (i.e., in the return() portion).
Rcode:
to get the standard error=standard deviation/sqrt(sample size),use sd function and length function.
calculateMeanVar <- function(x) {
x_mean <- mean(x)
x_var <- var(x)
x_sd <- sd(x)
x_len <- length(x)
x_SE <- x_sd/sqrt(x_len)
return(data.frame(xmean = x_mean, xvar = x_var,xSE=x_SE))
}
To test the above function on iris petal length:
calculateMeanVar(iris$Petal.Length)
Output:
petal length standard error=0.144136
Get Answers For Free
Most questions answered within 1 hours.