Public EcoRouter Knowledge · AI-generated content · September 28, 2026
How do I calculate the mean, median, and standard deviation of a variable in R?
The question
How do I calculate the mean, median, and standard deviation of a variable in R?
In R, you can use the built-in functions mean(), median(), and sd().
Here is a quick example:
# Create a sample vector
data <- c(10, 15, 20, 25, 30)
# Calculate statistics
mean_val <- mean(data)
median_val <- median(data)
sd_val <- sd(data)
# Print the results
mean_val # Returns 20
median_val # Returns 20
sd_val # Returns 7.905694Working with a Data Frame
If your variable is a column inside a data frame:
mean(df$variable_name)
median(df$variable_name)
sd(df$variable_name)Handling Missing Values (NA)
If your data contains missing values, these functions will return NA by default. Add na.rm = TRUE to ignore them:
data_with_na <- c(10, 15, NA, 25, 30)
mean(data_with_na, na.rm = TRUE)
median(data_with_na, na.rm = TRUE)
sd(data_with_na, na.rm = TRUE)Tip: You can also use summary(data) to get the mean, median, quartiles, and count of NAs all at once.
Answering this used an estimated 75% less than EcoRouter’s configured frontier baseline
- Routed to
- Balanced
- Tokens
- 211 in / 561 out
- Cost
- $0.0023
- Baseline
- $0.0090
Figures recorded by EcoRouter when this answer was generated, and fixed at that moment. Cost comparisons are against a configured reference model, not a measurement of electricity, carbon or water.
Ask about this
Ask EcoRouter a follow-up using this Knowledge as context. Nothing becomes public unless you choose to publish it.
Ask a follow-upComments
No comments yet.