Run a Welch two-group t-test in R and interpret the p-value

Quick Overview

Write an R function that runs a two-sided Welch t-test between the two groups of a data frame column and a helper that turns the p-value into a reject or fail-to-reject verdict. Tests NA handling, deterministic group order, reading t.test output and interpreting p-values.

Run a Welch two-group t-test in R and interpret the p-value

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `two_group_ttest(data, value_col, group_col, alpha = 0.05)` and `interpret_pvalue(p_value, alpha = 0.05)` in `/app/solution.R`. The first runs a Welch t-test comparing `value_col` across the two groups of `group_col`. The second maps a p-value to a verdict. - Drop rows where `value_col` or `group_col` is `NA`. `stop()` unless `group_col` has exactly two distinct non-NA levels. - `g1` and `g2` are the first and second sorted (ascending) levels. Run the two-sided base R `t.test(x_g1, y_g2)` with `var.equal = FALSE`. - Return a named list: `statistic`, `df`, `p_value`, `mean_diff`, `significant`, `conclusion`. - `mean_diff` and the direction of t are `mean(g1) - mean(g2)`. `significant` is `TRUE` if and only if `p_value < alpha`. - `conclusion` is `interpret_pvalue(p_value, alpha)`, which returns `"reject null hypothesis"` when `p_value < alpha`, else `"fail to reject null hypothesis"`. Example: ```r df <- data.frame( score = c(5.1, 6.2, 5.8, 7.0, 6.5, 7.3), grp = c("A", "A", "A", "B", "B", "B") ) res <- two_group_ttest(df, "score", "grp") res$mean_diff # -1.233333, mean(A) - mean(B) because A sorts before B res$statistic # about -3.105 res$df # about 3.65 (Welch-Satterthwaite) res$p_value # about 0.041, two-sided res$conclusion # "reject null hypothesis" ``` ```hint Fix the direction Settle the group order before calling `t.test`, then check which argument's mean the reported statistic subtracts from which. ``` ```hint Check the returned values `t.test` returns named numbers such as `t` and `df`. Look at what those names carry into the list you return, and at what `t.test` does when a group is too small. ``` ### Clarifying Questions to Ask - If the group column is numeric or a factor, should "sorted ascending" follow numeric order, factor level order, or the labels as text? - What should happen when one group has fewer than two observations after dropping `NA`? - Should a factor level that never appears in the data count as one of the two levels? ### What a Strong Answer Covers - Dropping incomplete rows first and validating that exactly two distinct groups remain. - A deterministic group order so the sign of `statistic` and `mean_diff` is reproducible. - Calling `t.test` with `var.equal = FALSE`, two-sided, and returning unnamed numeric values from the result. - A strict `p_value < alpha` rule shared by `significant` and `conclusion`. - Edge cases: groups with fewer than two values, constant data, numeric or factor group columns. ### Follow-up Questions - How would you extend this to report an effect size like Cohen's d? - When would Welch's test be preferable to the pooled-variance Student's t-test? - What would you use if the values were heavily skewed or had large outliers? - How should a "fail to reject null hypothesis" result be explained to a stakeholder?

Overview: Write an R function that runs a two-sided Welch t-test between the two groups of a data frame column and a helper that turns the p-value into a reject or fail-to-reject verdict. Tests NA handling, deterministic group order, reading t.test output and interpreting p-values.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement two_group_ttest(data, value_col, group_col, alpha = 0.05) and interpret_pvalue(p_value, alpha = 0.05) in /app/solution.R. The first runs a Welch t-test comparing value_col across the two groups of group_col. The second maps a p-value to a verdict.

  • Drop rows where value_col or group_col is NA . stop() unless group_col has exactly two distinct non-NA levels.
  • g1 and g2 are the first and second sorted (ascending) levels. Run the two-sided base R t.test(x_g1, y_g2) with var.equal = FALSE .
  • Return a named list: statistic , df , p_value , mean_diff , significant , conclusion .
  • mean_diff and the direction of t are mean(g1) - mean(g2) . significant is TRUE if and only if p_value < alpha .
  • conclusion is interpret_pvalue(p_value, alpha) , which returns "reject null hypothesis" when p_value < alpha , else "fail to reject null hypothesis" .

Example:

df <- data.frame(
  score = c(5.1, 6.2, 5.8, 7.0, 6.5, 7.3),
  grp   = c("A", "A", "A", "B", "B", "B")
)
res <- two_group_ttest(df, "score", "grp")
res$mean_diff     # -1.233333, mean(A) - mean(B) because A sorts before B
res$statistic     # about -3.105
res$df            # about 3.65 (Welch-Satterthwaite)
res$p_value       # about 0.041, two-sided
res$conclusion    # "reject null hypothesis"

Clarifying Questions to Ask Guidance

  • If the group column is numeric or a factor, should "sorted ascending" follow numeric order, factor level order, or the labels as text?
  • What should happen when one group has fewer than two observations after dropping NA ?
  • Should a factor level that never appears in the data count as one of the two levels?

What a Strong Answer Covers Guidance

  • Dropping incomplete rows first and validating that exactly two distinct groups remain.
  • A deterministic group order so the sign of statistic and mean_diff is reproducible.
  • Calling t.test with var.equal = FALSE , two-sided, and returning unnamed numeric values from the result.
  • A strict p_value < alpha rule shared by significant and conclusion .
  • Edge cases: groups with fewer than two values, constant data, numeric or factor group columns.

Follow-up Questions Guidance

  • How would you extend this to report an effect size like Cohen's d?
  • When would Welch's test be preferable to the pooled-variance Student's t-test?
  • What would you use if the values were heavily skewed or had large outliers?
  • How should a "fail to reject null hypothesis" result be explained to a stakeholder?