This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

# Generate age data
age <- c(25, 30, 35, 40, 45, 50, 55, 60, 65, 70)

# Generate corresponding height data
height <- c(160, 165, 170, 175, 180, 185, 190, 195, 200, 205)

# Calculate xy
age_height <- age * height

# Calculate the sum of xy
sum_age_height <- sum(age_height)

# Calculate n (number of data points)
n <- length(age)

# Calculate n * sum_xy
n_times_sum_age_height <- n * sum_age_height

# Calculate sum of age
sum_age <- sum(age)

# Calculate sum of height
sum_height <- sum(height)

# Calculate sum of age * sum of height
sum_age_times_sum_height <- sum_age * sum_height

# Calculate the numerator
num <- n * sum_age_height - sum_age * sum_height

# Calculate sq of age
age_sq <- age^2

# Calculate sum of sq of age
sum_age_sq <- sum(age_sq)

# Calculate n * sum of sq of age
n_times_sum_age_sq <- n * sum_age_sq

# Calculate sq of sum age
sq_sum_age <- sum_age^2

# Calculate sq of height
height_sq <- height^2

# Calculate sum of sq of height
sum_height_sq <- sum(height_sq)

# Calculate n * sum of sq of height
n_times_sum_height_sq <- n * sum_height_sq

# Calculate sq of sum height
sq_sum_height <- sum_height^2

# Calculate the denominator
deno <- ((n * sum_age_sq) - sq_sum_age) * ((n * sum_height_sq) - sq_sum_height)

# Calculate the square root of the denominator
sq_root_deno <- deno^0.5

# Calculate the correlation coefficient (R)
r <- num / sq_root_deno

# Output the correlation coefficient
r
## [1] 1