1---
2name: r
3description: Avoid common R mistakes — vectorization traps, NA propagation, factor surprises, and indexing gotchas.
4metadata:
5 source: https://skillmd.com/skills/modbender/r
6 clawdbot:
7 emoji: 📊
8 requires:
9 bins:
10 - Rscript
11 os:
12 - linux
13 - darwin
14 - win32
15---
16
17## Vectorization
18- Loops are slow — use `apply()`, `lapply()`, `sapply()`, or `purrr::map()`
19- Vectorized functions operate on whole vectors — `sum(x)` not `for (i in x) total <- total + i`
20- `ifelse()` is vectorized — `if` is not, use `ifelse()` for vector conditions
21- Column operations faster than row — R is column-major
22
23## Indexing Gotchas
24- R is 1-indexed — first element is `x[1]`, not `x[0]`
25- `x[0]` returns empty vector — not error, silent bug
26- Negative index excludes — `x[-1]` removes first element
27- `[[` extracts single element — `[` returns subset (list stays list)
28- `df[, 1]` drops to vector — use `df[, 1, drop = FALSE]` to keep data frame
29
30## NA Handling
31- NA propagates — `1 + NA` is `NA`, `NA == NA` is `NA`
32- Use `is.na()` to check — not `x == NA`
33- Most functions need `na.rm = TRUE` — `mean(x)` returns NA if any NA present
34- `na.omit()` removes rows with any NA — may lose data unexpectedly
35- `complete.cases()` returns logical vector — rows without NA
36
37## Factor Traps
38- Old R converted strings to factors by default — use `stringsAsFactors = FALSE` or modern R
39- `levels()` shows categories — but factor values are integers internally
40- Adding new value not in levels gives NA — use `factor(x, levels = c(old, new))`
41- `as.numeric(factor)` gives level indices — use `as.numeric(as.character(factor))` for values
42- Dropping unused levels: `droplevels()` — or `factor()` again
43
44## Recycling
45- Shorter vector recycled to match longer — `c(1,2,3) + c(10,20)` gives `11, 22, 13`
46- No error if lengths aren't multiples — just warning, easy to miss
47- Single values recycle intentionally — `x + 1` adds 1 to all elements
48
49## Data Frames vs Tibbles
50- Tibble never converts strings to factors — safer defaults
51- Tibble never drops dimensions — `df[, 1]` stays tibble
52- Tibble prints better — shows type, doesn't flood console
53- `as_tibble()` to convert — from `tibble` or `dplyr` package
54
55## Assignment
56- `<-` is idiomatic R — `=` works but avoided in style guides
57- `<<-` assigns to parent environment — global assignment, usually a mistake
58- `->` right assignment exists — rarely used, confusing
59
60## Scope
61- Functions look up in parent environment — can accidentally use global variable
62- Local variable shadows global — same name hides outer variable
63- `local()` creates isolated scope — variables don't leak out
64
65## Common Mistakes
66- `T` and `F` can be overwritten — use `TRUE` and `FALSE` always
67- `1:length(x)` fails on empty x — gives `c(1, 0)`, use `seq_along(x)`
68- `sample(5)` vs `sample(c(5))` — different! first gives 1:5 permutation
69- String splitting: `strsplit()` returns list — even for single string