Bird
Raised Fist0
R Programmingprogramming~20 mins

t-test in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
t-test Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a one-sample t-test in R
What is the output of this R code snippet?
R Programming
set.seed(123)
x <- rnorm(10, mean=5, sd=2)
t.test.result <- t.test(x, mu=5)
round(t.test.result$p.value, 3)
A0.564
B0.045
C0.123
D0.001
Attempts:
2 left
💡 Hint
Check the p-value from the t.test object and round it to 3 decimals.
🧠 Conceptual
intermediate
2:00remaining
Understanding paired t-test in R
Which option correctly describes what this R code does?
R Programming
before <- c(200, 195, 210, 190, 205)
after <- c(198, 192, 215, 188, 207)
t.test(before, after, paired=TRUE)
APerforms a paired t-test comparing means of before and after measurements on the same subjects.
BPerforms an independent two-sample t-test assuming equal variances.
CPerforms a one-sample t-test comparing before to zero.
DPerforms a paired t-test but ignores pairing and treats data as independent.
Attempts:
2 left
💡 Hint
Look at the paired=TRUE argument and the two vectors before and after.
🔧 Debug
advanced
2:00remaining
Identify the error in this two-sample t-test code
What error will this R code produce?
R Programming
group1 <- c(5, 7, 8, 6)
group2 <- c(6, 9, 7)
t.test(group1, group2, paired=TRUE)
AError: object 'paired' not found
BError: 'x' and 'y' must have the same length for paired t-test
CNo error, runs successfully and returns p-value
DError: cannot perform t-test on numeric vectors
Attempts:
2 left
💡 Hint
Check the lengths of group1 and group2 when paired=TRUE.
❓ Predict Output
advanced
2:00remaining
Output of Welch Two Sample t-test in R
What is the output of this R code snippet?
R Programming
set.seed(42)
g1 <- rnorm(15, mean=10, sd=3)
g2 <- rnorm(20, mean=12, sd=5)
test <- t.test(g1, g2)
round(test$statistic, 2)
A-2.30
B1.45
C-1.45
D2.30
Attempts:
2 left
💡 Hint
Check the t-statistic value from the test object and round to 2 decimals.
📝 Syntax
expert
2:00remaining
Identify the syntax error in this t-test code
Which option contains the syntax error that will cause R to fail running the code?
At.test(x, mu=5, alternative='two.sided')
Bt.test(x, mu=5, alternative='greater')
Ct.test(x, mu=5, alternative='less')
Dt.test(x, mu=5 alternative='greater')
Attempts:
2 left
💡 Hint
Look for missing commas between arguments.

Practice

(1/5)
1.

What does a t-test primarily compare in two groups?

easy
A. The difference between their medians
B. The difference between their averages
C. The total number of observations
D. The variance of each group

Solution

  1. Step 1: Understand the purpose of a t-test

    A t-test is used to compare the means (averages) of two groups to see if they are different.
  2. Step 2: Identify what is compared

    The test focuses on the average values, not medians, counts, or variances.
  3. Final Answer:

    The difference between their averages -> Option B
  4. Quick Check:

    t-test compares means = A [OK]
Hint: Remember: t-test checks averages, not medians or counts [OK]
Common Mistakes:
  • Confusing mean with median
  • Thinking it compares counts
  • Assuming it compares variances
2.

Which of the following is the correct way to run a t-test in R comparing two numeric vectors x and y?

easy
A. t_test(x, y)
B. ttest(x, y)
C. t.test(x, y)
D. test.t(x, y)

Solution

  1. Step 1: Recall the correct function name in R

    The built-in function for t-test in R is t.test().
  2. Step 2: Check the syntax for comparing two vectors

    Using t.test(x, y) compares the two numeric vectors correctly.
  3. Final Answer:

    t.test(x, y) -> Option C
  4. Quick Check:

    Correct function is t.test() = A [OK]
Hint: Use exact function name t.test() with dot [OK]
Common Mistakes:
  • Misspelling the function name
  • Using underscores instead of dots
  • Confusing function with other test names
3.

What will be the output of this R code snippet?

group1 <- c(5, 7, 8, 6, 9)
group2 <- c(10, 12, 11, 13, 14)
test <- t.test(group1, group2)
round(test$p.value, 3)
medium
A. 0.001
B. 0.05
C. 0.5
D. 1.0

Solution

  1. Step 1: Understand the data groups

    Group1 has smaller values around 5-9, Group2 has larger values around 10-14, so means differ.
  2. Step 2: Interpret the p-value from t.test()

    Since the groups differ clearly, the p-value will be very small, close to 0.001 after rounding.
  3. Final Answer:

    0.001 -> Option A
  4. Quick Check:

    Distinct groups give low p-value = B [OK]
Hint: Big difference in groups means very small p-value [OK]
Common Mistakes:
  • Assuming p-value is large for different groups
  • Confusing p-value with test statistic
  • Ignoring rounding in output
4.

Identify the error in this R code for a t-test:

data1 <- c(4, 5, 6)
data2 <- c(7, 8, 9)
test <- t.test(data1 + data2)
medium
A. t.test() cannot compare numeric vectors
B. Missing parentheses in t.test()
C. Vectors data1 and data2 must be data frames
D. Using '+' instead of ',' to separate vectors

Solution

  1. Step 1: Check the function call syntax

    The t.test() function expects two separate vectors as arguments separated by a comma.
  2. Step 2: Identify the mistake in argument passing

    The code uses '+' to add vectors instead of separating them with a comma, which is incorrect.
  3. Final Answer:

    Using '+' instead of ',' to separate vectors -> Option D
  4. Quick Check:

    Arguments must be comma-separated = D [OK]
Hint: Separate vectors with comma, not plus sign [OK]
Common Mistakes:
  • Adding vectors instead of passing separately
  • Confusing function arguments syntax
  • Thinking t.test needs data frames
5.

You have two groups of exam scores: before and after a training program. Which t-test is best to check if the training improved scores?

hard
A. Paired t-test using t.test(before, after, paired=TRUE)
B. Two-sample t-test using t.test(before, after)
C. One-sample t-test using t.test(after)
D. Variance test using var.test(before, after)

Solution

  1. Step 1: Understand the data relationship

    Scores are from the same students before and after training, so data are paired.
  2. Step 2: Choose the correct t-test type

    A paired t-test compares matched pairs to see if scores changed after training.
  3. Final Answer:

    Paired t-test using t.test(before, after, paired=TRUE) -> Option A
  4. Quick Check:

    Paired data needs paired=TRUE = C [OK]
Hint: Use paired=TRUE for before-after same subjects [OK]
Common Mistakes:
  • Using two-sample test for paired data
  • Using one-sample test incorrectly
  • Confusing variance test with t-test
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