Skip to main content

Translation Traps

AI translation is useful, but it often hides small language differences. These are the traps to check first.

Trap 1: Indexing

R starts counting at 1. Python starts counting at 0.

values <- c(10, 20, 30)
values[1]
values = [10, 20, 30]
values[0]

Python slices are end-exclusive:

values[0:2]

This returns the first two values, not the first three.

Walk through it position by position. Python labels the items position 0 (10), position 1 (20), and position 2 (30). The slice values[0:2] starts at position 0 and stops just before position 2 — that is what “end exclusive” means: the end position itself is left out. So you get 10 and 20, but not 30. The matching R code is values[1:2], because R starts counting at 1 and includes both ends. This off-by-one difference is one of the most common AI translation bugs, so always count a small example by hand.

Trap 2: Indentation

R uses braces to mark code blocks. Python uses indentation.

if (age >= 20) {
  "adult"
} else {
  "not in adult cohort"
}
if age >= 20:
    "adult"
else:
    "not in adult cohort"

If Python indentation is inconsistent, the code may fail or run with a different meaning.

Trap 3: Assignment vs. Comparison

Meaning R Python
assign a value x <- 10 x = 10
compare values x == 10 x == 10
missing/null check is.na(x) pd.isna(x) or x is None

Trap 4: Missing Values

R and Python both have missing values, but they are handled by different functions. Always check missingness before comparing summaries.

sum(is.na(nhanes$BMI))
nhanes["BMI"].isna().sum()

Audit Habit

When a translated result differs, check indexing, indentation, assignment/comparison, missingness handling, and grouping variables before assuming one language is “wrong.”

Video walkthrough (optional)