Check data

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

assert

Stop when a condition fails

Stata

assert x > 0

R Package:tidystata

assert(data, x > 0)

Differs from Stata Reports the number of contradictions and returns the data, so it fits in a pipe. Missing x counts as a contradiction, unlike Stata, where missing is larger than any number: write assert(data, is.na(x) | x > 0) for Stata’s behaviour.

confirm

Check that a variable exists

Stata

confirm variable x

R Package:base

stopifnot("x" %in% names(data))

count

Number of rows

Stata

count

R Package:base

nrow(data)
1 more examples and alternatives

Count rows meeting a condition

Stata

count if x > 0

R Package:base

sum(data$x > 0, na.rm = TRUE)

Differs from Stata Stata numeric missing values compare larger than every finite number; R comparisons return NA. Handle missing values explicitly before applying the condition.

distinct

Number of distinct combinations

Stata

distinct x1 x2, joint

R Package:dplyr

nrow(distinct(data, x1, x2))

duplicates

List duplicated rows

Stata

duplicates list x1 x2

R Package:tidystata

duplicates_list(data, x1, x2)

Count duplicated rows

Stata

duplicates report x1 x2

R Package:tidystata

duplicates_report(data, x1, x2)

Mark duplicated rows

Stata

duplicates tag x1 x2, generate(dup)

R Package:tidystata

duplicates_tag(data, x1, x2, .name = "dup")

isid

Check that variables identify the rows

Stata

isid id wave

R Package:tidystata

isid(data, id, wave)

mdesc

Missing values per variable

Stata

mdesc

R Package:tidystata

summ(data, .stat = "missing")

misstable

Missing-value patterns

Stata

misstable patterns

R Package:tidystata

misstable(data, .patterns = TRUE)

1 marks an observed value and 0 a missing one, as in Stata.

Missing values per variable

Stata

misstable summarize

R Package:tidystata

misstable(data)

Base R: colSums(is.na(data)).