Bird
Raised Fist0
R Programmingprogramming~20 mins

Correlation analysis in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Calculate Pearson correlation coefficient
What is the output of this R code snippet that calculates the Pearson correlation between two numeric vectors?
R Programming
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
correlation <- cor(x, y, method = "pearson")
print(round(correlation, 2))
A0.5
B0
C-1
D1
Attempts:
2 left
💡 Hint
Pearson correlation measures linear relationship strength between two variables.
❓ Predict Output
intermediate
2:00remaining
Spearman correlation with tied ranks
What is the output of this R code that calculates Spearman correlation for vectors with tied values?
R Programming
a <- c(1, 2, 2, 3, 4)
b <- c(5, 6, 6, 7, 8)
result <- cor(a, b, method = "spearman")
print(round(result, 2))
A1
B0.9
C0.7
D0.5
Attempts:
2 left
💡 Hint
Spearman correlation uses ranks and handles ties by averaging ranks.
❓ Predict Output
advanced
2:00remaining
Correlation matrix with NA values
What is the output of this R code that computes correlation matrix with missing values?
R Programming
data <- data.frame(
  v1 = c(1, 2, NA, 4),
  v2 = c(2, NA, 6, 8),
  v3 = c(5, 6, 7, 8)
)
result <- cor(data, use = "complete.obs")
print(round(result, 2))
A
[[1,1]] 1, [[1,2]] 0.98, [[1,3]] 0.99
[[2,1]] 0.98, [[2,2]] 1, [[2,3]] 0.98
[[3,1]] 0.99, [[3,2]] 0.98, [[3,3]] 1
B
[[1,1]] 1, [[1,2]] 0.99, [[1,3]] 1
[[2,1]] 0.99, [[2,2]] 1, [[2,3]] 0.99
[[3,1]] 1, [[3,2]] 0.99, [[3,3]] 1
C
[[1,1]] 1, [[1,2]] NA, [[1,3]] 1
[[2,1]] NA, [[2,2]] 1, [[2,3]] NA
[[3,1]] 1, [[3,2]] NA, [[3,3]] 1
D
[[1,1]] 1, [[1,2]] 1, [[1,3]] 1
[[2,1]] 1, [[2,2]] 1, [[2,3]] 1
[[3,1]] 1, [[3,2]] 1, [[3,3]] 1
Attempts:
2 left
💡 Hint
Using 'complete.obs' excludes rows with any NA in any variable pair.
❓ Predict Output
advanced
2:00remaining
Partial correlation calculation
What is the output of this R code that calculates the partial correlation between x and y controlling for z?
R Programming
library(ppcor)
x <- c(1, 2, 3, 4, 5)
y <- c(2, 1, 4, 3, 5)
z <- c(5, 4, 3, 2, 1)
result <- pcor.test(x, y, z)
print(round(result$estimate, 2))
A-0.9
B-0.4
C0.0
D0.8
Attempts:
2 left
💡 Hint
Partial correlation measures relationship between x and y after removing effect of z.
🧠 Conceptual
expert
3:00remaining
Interpreting correlation significance test output
Given this R output from cor.test(x, y), which statement correctly interprets the p-value and correlation estimate?
R Programming
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(2, 1, 4, 3, 5, 7, 6, 8, 9, 10)
test <- cor.test(x, y)
print(test$p.value)
print(round(test$estimate, 2))
AThe correlation is about 0.03 and is statistically significant at 0.05 level.
BThe correlation is about 0.97 but not statistically significant at 0.05 level.
CThe correlation is about 0.97 and is statistically significant at 0.05 level.
DThe correlation is about 0.03 and not statistically significant at 0.05 level.
Attempts:
2 left
💡 Hint
Check both the correlation estimate and the p-value to interpret significance.

Practice

(1/5)
1. What does the cor() function in R calculate?
easy
A. The correlation coefficient between two numeric vectors
B. The sum of two numeric vectors
C. The difference between two numeric vectors
D. The product of two numeric vectors

Solution

  1. Step 1: Understand the purpose of cor()

    The cor() function is designed to calculate how two numeric vectors move together, which is called correlation.
  2. Step 2: Identify the correct description

    Correlation coefficient measures the strength and direction of a linear relationship between two numeric vectors.
  3. Final Answer:

    The correlation coefficient between two numeric vectors -> Option A
  4. Quick Check:

    cor() calculates correlation [OK]
Hint: Remember: cor() finds how two numbers move together [OK]
Common Mistakes:
  • Confusing correlation with sum or difference
  • Thinking cor() calculates causation
  • Using cor() on non-numeric data
2. Which of the following is the correct syntax to calculate correlation between vectors x and y in R?
easy
A. correlation_coefficient(x, y)
B. cor(x, y)
C. correlate(x, y)
D. correlation(x, y)

Solution

  1. Step 1: Recall the R function name

    The correct function to calculate correlation in R is cor().
  2. Step 2: Check the syntax

    The function takes two numeric vectors as arguments: cor(x, y).
  3. Final Answer:

    cor(x, y) -> Option B
  4. Quick Check:

    Use cor() with two vectors [OK]
Hint: Use exactly cor(x, y) to get correlation [OK]
Common Mistakes:
  • Using incorrect function names like correlation()
  • Adding extra arguments unnecessarily
  • Using wrong argument order
3. What is the output of the following R code?
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
cor(x, y)
medium
A. 1
B. -1
C. 0
D. NA

Solution

  1. Step 1: Analyze the vectors

    Vector y is exactly twice x for each element, so they have a perfect positive linear relationship.
  2. Step 2: Calculate correlation

    Perfect positive linear relationship means correlation coefficient is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Perfect positive correlation = 1 [OK]
Hint: If one vector is a perfect multiple of another, cor = 1 [OK]
Common Mistakes:
  • Confusing correlation with sum or mean
  • Expecting 0 when vectors are related
  • Misreading negative correlation
4. Identify the error in this R code for correlation:
x <- c(1, 2, 3)
y <- c(4, 5)
cor(x, y)
medium
A. cor() function is misspelled
B. Vectors contain non-numeric data
C. Vectors have different lengths causing an error
D. No error, code runs fine

Solution

  1. Step 1: Check vector lengths

    Vector x has length 3, vector y has length 2, so lengths differ.
  2. Step 2: Understand cor() requirements

    cor() requires vectors of equal length to compute correlation.
  3. Final Answer:

    Vectors have different lengths causing an error -> Option C
  4. Quick Check:

    cor() needs equal length vectors [OK]
Hint: Vectors must be same length for cor() [OK]
Common Mistakes:
  • Ignoring vector length mismatch
  • Assuming cor() auto-adjusts lengths
  • Thinking cor() works with non-numeric data
5. You have two numeric vectors a and b with some missing values (NA). Which is the correct way to calculate correlation ignoring missing values in R?
hard
A. cor(a, b, removeNA = TRUE)
B. cor(a, b, na.rm = TRUE)
C. cor(a, b, method = "ignoreNA")
D. cor(a, b, use = "complete.obs")

Solution

  1. Step 1: Understand handling of missing values in cor()

    The cor() function uses the use argument to specify how to handle missing values.
  2. Step 2: Identify correct argument value

    Setting use = "complete.obs" tells R to use only pairs without any NA values.
  3. Final Answer:

    cor(a, b, use = "complete.obs") -> Option D
  4. Quick Check:

    Use use = "complete.obs" to ignore NA [OK]
Hint: Use use="complete.obs" to skip NA pairs [OK]
Common Mistakes:
  • Using na.rm which is not an argument for cor()
  • Using incorrect argument names like removeNA
  • Assuming method argument handles NA
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard