Correlation analysis helps us find out if two things change together and how strong their relationship is.
0
0
Correlation analysis in R Programming
Introduction
To check if studying hours and exam scores are related.
To see if temperature and ice cream sales go up or down together.
To find out if height and weight have a connection.
To explore if advertising budget affects product sales.
Syntax
R Programming
cor(x, y, method = "pearson", use = "everything")
x and y are numeric vectors or columns of data.
method can be "pearson" (default), "spearman", or "kendall" depending on the type of correlation.
Examples
Calculates Pearson correlation between
x and y.R Programming
cor(x, y)
Calculates Spearman rank correlation, useful for non-linear relationships.
R Programming
cor(x, y, method = "spearman")Ignores missing values when calculating correlation.
R Programming
cor(x, y, use = "complete.obs")Sample Program
This program creates two numeric vectors x and y where y is exactly twice x. It then calculates the Pearson correlation and prints it.
R Programming
x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 6, 8, 10) result <- cor(x, y) print(result)
OutputSuccess
Important Notes
Correlation values range from -1 to 1.
A value close to 1 means strong positive relationship, close to -1 means strong negative relationship, and around 0 means no relationship.
Correlation does not mean one thing causes the other.
Summary
Correlation analysis measures how two numeric things move together.
Use cor() function in R to find correlation.
Remember, correlation shows relationship strength, not cause.