Q1
What subtle vector-recycling bug happens when running c(1, 2, 3, 4) + c(10, 20) vs c(1, 2, 3) + c(10, 20) in R?
R silently recycles shorter vectors! When the longer length is an exact multiple (4 vs 2), R adds c(10, 20, 10, 20) with ZERO warnings; when 3 vs 2, it still computes with only a non-fatal warning—causing silent math bugs.
Q2
Why does extracting df[, 'col'] return a vector on multi-column data.frames, while df[, c('a', 'b')] returns a data.frame?
Base R data.frame defaults to drop = TRUE, collapsing 1-column selections down into atomic vectors—which breaks downstream functions expecting 2D data frames! Use drop = FALSE or tibble (dplyr::select()), which never drops dimensions unexpectedly.
Q3
Why does as.numeric(factor_var) on a factor containing numeric labels (c('10', '20')) return 1, 2 instead of 10, 20?
R factors store underlying integer codes (1, 2, ...) mapped alphabetically to character levels. Calling as.numeric() directly reads the internal integer codes! Always convert via as.numeric(as.character(factor_var)).
Q4
What is the key difference between lapply(), sapply(), and vapply() inside production R scripts?
lapply always returns a list; sapply tries to simplify output into a vector/matrix (dangerously unpredictable if one element varies in length!); vapply enforces an explicit return template (FUN.VALUE), catching unexpected types immediately.
Q5
Why does R copy objects lazily (Copy-on-Modify)—and why does growing a vector inside a for loop (res <- c(res, x)) become O(n^2) slow?
Appending inside a loop copies the entire vector on every single iteration. Pre-allocate output vectors upfront (vector('numeric', n)) or use vectorized/purrr functions.
Q6
In dplyr, why do grouped calculations (group_by(dept) %>% mutate(...)) behave differently if you forget ungroup() afterwards?
Remaining grouping attributes silently apply row-wise or group-wise scopes to future summarise(), slice(), or mutate() steps downstream. Always add .groups = 'drop' or ungroup() cleanly.
Q7
Why does NA == NA return NA instead of TRUE in R—and why does x == NA fail in filter()?
NA represents an unknown value; comparing unknowns yields unknown (NA). Always test missingness with is.na(x) (or %in% which handles comparisons cleanly without returning NA).
Q8
In ggplot2, why does aes(color = 'blue') draw RED points instead of blue?
Anything inside aes(...) maps data values to scales, so R treats 'blue' as a 1-level categorical variable assigned ggplot's first default hue (coral red)! Static styling colors go *outside* aes(), directly inside geom_point(color = 'blue').
Q9
How do data.table := in-place updates avoid full-table copies compared to dplyr on 10M+ rows?
data.table mutates columns directly by reference in memory (DT[, new := a + b]), avoiding full data-frame reallocation when transforming multi-gigabyte datasets.
Q10
Why is 0.1 + 0.2 == 0.3 FALSE in R, and what function prevents floating-point test failures?
IEEE-754 binary floating-point rounding produces 0.30000000000000004. Compare floats with dplyr::near(a, b) or all.equal(a, b) using tolerance rather than ==.