The “var” Function in R
Package: Base R (no specific package required)
Purpose: Calculates the sample variance of a numeric vector.
General Class: Descriptive Statistics
Required Argument(s):
x: A numeric vector.
Notable Optional Arguments:
na.rm: A logical value indicating whether NA values should be stripped before the computation proceeds. The default is FALSE.
Example:
# Example vector
data <- c(2, 4, 6, 8, 10, NA)
# Calculate sample variance excluding NA values
result <- var(data, na.rm = TRUE)
print(result)In this example, the var function is used to calculate the sample variance of the numeric vector data. The na.rm argument is set to TRUE to exclude NA values from the calculation. The result is then printed to the console.