R — Statistics
Descriptive statistics
data <- c(12, 15, 18, 22, 25, 28, 30, 35, 40, 45)
mean(data) # 27
median(data) # 26.5
sd(data) # Standard deviation
var(data) # Variance
range(data) # 12 45
diff(range(data)) # 33
Summary
summary(data)
# For data frames
summary(mtcars)
Correlation
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 5, 4, 5)
cor(x, y) # Correlation coefficient
cor.test(x, y) # Full test
Hypothesis testing
# t-test
t.test(rnorm(100, mean = 5), mu = 5)
# Chi-squared test
chisq.test(table(c("A", "B", "A", "B", "A")))
# ANOVA
anova(lm(mpg ~ factor(cyl), data = mtcars))
Linear regression
model <- mtcars$mpg ~ mtcars$wt
lm_model <- lm(model)
summary(lm_model)
# Predict
predict(lm_model, data.frame(wt = 3.0))
Mini Practice
Write R code that:
- Computes descriptive statistics
- Calculates correlation
- Performs a t-test
- Fits a linear regression model
Up Next
In the next lesson, you'll learn about Visualization — creating charts with ggplot2.
Related Topics
Frequently Asked Questions about Statistics
What is Statistics in R?
Statistics is a fundamental concept in R. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Statistics?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Statistics.
Why is Statistics important in R?
Statistics is essential for R development. Understanding this concept will help you write better code and solve real-world problems more effectively.