Skip to course content
Free R statistics course

Statistical Data Analytics with R

Unit 02.01: The variable's type decides the test

The variable's type decides the test. Getting the type wrong is how somebody ends up running a t-test on a yes/no answer.

Six kinds of variable, and what each admits

Numeric values support means, standard deviations and t-tests. Categorical values support counts, proportions and chi-square. Ordinal values have a real order but no meaningful spacing, so averaging them asserts something the measurement does not support.

Counts are non-negative integers, often skewed, and a symmetric interval around their mean can extend below zero. Rates are counts divided by exposure and are meaningless without the denominator. Binary outcomes are the special case behind proportions, chi-square and logistic regression.

In R, the class is not a formality: a grouping variable left as text gets an alphabetical reference level, and an ordinal factor needs ordered = TRUE to behave as ordered.

This block builds one of each and reports its class.

suppressPackageStartupMessages(library(dplyr))

d <- data.frame(
  revenue   = c(31.5, 44.0, 12.9),                       # numeric, continuous
  browser   = factor(c("chrome", "safari", "chrome")),    # categorical, unordered
  rating    = factor(c("low", "high", "medium"),          # ordinal
                     levels = c("low", "medium", "high"), ordered = TRUE),
  clicks    = c(3L, 11L, 0L),                            # count
  abandoned = c(TRUE, FALSE, TRUE)                       # binary
)
d$rate <- round(d$clicks / c(120, 300, 45), 4)           # rate: count / exposure

for (nm in names(d)) {
  cat(sprintf("%-10s %-12s ordered=%s\n", nm, class(d[[nm]])[1],
              is.ordered(d[[nm]])))
}

cat("\nWhy the type decides the test:\n")
cat("  binary outcome  -> proportions, chi-square, logistic regression\n")
cat("  count outcome   -> counts are non-negative integers; a mean of",
    round(mean(d$clicks), 2), "can be fine, a normal interval around it may not\n")
cat("  rate outcome    -> must carry its denominator;", d$clicks[1],
    "clicks in 120 views is a rate of", d$rate[1], "-- the same", d$clicks[1],
    "clicks in 12 views would be", round(d$clicks[1] / 12, 4), "\n")
cat("  ordinal         -> the order is real, the spacing is not\n")

Six variables, six classes: numeric, factor, ordered factor, integer, logical and a derived numeric rate. Only rating reports ordered = TRUE. The rate line makes the denominator point concrete: 3 clicks in 120 views is a rate of 0.025, while the same 3 clicks in 12 views is 0.25 — ten times larger from an identical numerator.

The mistake this prevents

The mistake is averaging an ordinal scale. The mean of 'low, medium, high' coded 1, 2, 3 assumes the step from low to medium equals the step from medium to high, which nothing about the measurement guarantees.

Takeaway

Classify every variable before choosing a test. Make grouping variables factors with deliberate levels, mark ordinal ones as ordered, and never report a rate without its denominator.