typestar

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

  1. cor computes Pearson by default, Spearman on request.
  2. cor.test adds a p-value and confidence interval.
  3. Passing a data frame returns a correlation matrix.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 4 in Correlation & regression, step 6 of 11 in Statistics.

← Previous Next →