Correlation in R
Measuring how two variables move together.
set.seed(3)
hours <- rnorm(50, mean = 20, sd = 5)
score <- 40 + 2.5 * hours + rnorm(50, sd = 8)
pearson <- cor(hours, score)
spearman <- cor(hours, score, method = "spearman")
covariance <- cov(hours, score)
test <- cor.test(hours, score)
p_value <- test$p.value
frame <- data.frame(hours, score)
matrix_of_cors <- cor(frame)
How it works
corcomputes Pearson by default, Spearman on request.cor.testadds a p-value and confidence interval.- Passing a data frame returns a correlation matrix.
Keywords and builtins used here
corcovrnorm
The run, in numbers
- Lines
- 13
- Characters to type
- 328
- Tokens
- 90
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 54 seconds.
Step 1 of 4 in Correlation & regression, step 6 of 11 in Statistics.