DAAF-Contribution-Community/daaf / .claude/skills/stata-r-translation
stata-r-translation
VerifiedStata-to-R translation for data analysis. Maps Stata commands (reghdfe, xtreg, ivregress, margins, esttab, svy:) to R equivalents (fixest, plm, survey, marginaleffects, modelsummary). Use when user has Stata background or requests Stata-equivalent code comments in R pipelines.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 26.1 KB |
| references/causal-inference.md | markdown | 6.9 KB |
| references/data-management.md | markdown | 8.6 KB |
| references/external-resources.md | markdown | 4.3 KB |
| references/gotchas.md | markdown | 6.6 KB |
| references/paradigm-differences.md | markdown | 11.2 KB |
| references/regression-modeling.md | markdown | 8.8 KB |
| references/strings-dates-labels.md | markdown | 4.7 KB |
| references/survey-spatial-ml.md | markdown | 3.3 KB |
| references/visualization.md | markdown | 3.6 KB |
| references/workflow-environment.md | markdown | 4.4 KB |
references/strings-dates-labels.md
View on GitHub# Strings, Dates, and Labels: Stata to R (stringr/lubridate/forcats)
> **Companion file:** For core data management (generate, replace, keep/drop,
> sorting, group operations, merging, reshaping, collapse, duplicates, missing
> values), see `data-management.md`.
---
## String Operations (stringr)
All R string operations via stringr follow the pattern `str_*(string, pattern)`.
| Stata | R (stringr) | Notes |
|-------|-------------|-------|
| `strpos(s, "pat") > 0` | `str_detect(s, "pat")` | Returns logical |
| `regexm(s, "pat")` | `str_detect(s, "pat")` | Returns logical |
| `subinstr(s, "old", "new", 1)` | `str_replace(s, "old", "new")` | First match |
| `subinstr(s, "old", "new", .)` | `str_replace_all(s, "old", "new")` | All matches |
| `substr(s, 1, 3)` | `str_sub(s, 1, 3)` | Both 1-indexed |
| `regexs(1)` after `regexm` | `str_extract(s, "(pat)")` or `str_match(s, "(pat)")[,2]` | |
| `strlower(s)` | `str_to_lower(s)` | |
| `strupper(s)` | `str_to_upper(s)` | |
| `strproper(s)` | `str_to_title(s)` | |
| `strtrim(s)` | `str_trim(s)` | Both ends |
| `strlen(s)` | `str_length(s)` | Character count |
| `word(s, 1)` | `word(s, 1)` | stringr also has `word()` |
| `s1 + " " + s2` | `paste(s1, s2)` or `paste0(s1, " ", s2)` | |
| `string(n)` | `as.character(n)` | |
| `real(s)` | `as.numeric(s)` | |
```stata
gen clean_name = strproper(strtrim(strlower(name)))
```
```r
df <- df |> mutate(clean_name = str_to_title(str_trim(str_to_lower(name))))
# Or with pipe:
df <- df |> mutate(clean_name = name |> str_to_lower() |> str_trim() |> str_to_title())
```
---
## Date System (lubridate)
Stata dates are integers from 1960-01-01 epoch. R Date objects are integers
from 1970-01-01, but this difference is hidden -- R displays human-readable dates.
| Stata | R (lubridate) | Notes |
|-------|---------------|-------|
| `date("2024-03-15", "YMD")` | `ymd("2024-03-15")` | |
| `date("March 15, 2024", "MDY")` | `mdy("March 15, 2024")` | |
| `mdy(3, 15, 2024)` | `make_date(2024, 3, 15)` | |
| `year(date)` | `year(date)` | |
| `month(date)` | `month(date)` | |
| `day(date)` | `day(date)` | |
| `dow(date)` | `wday(date)` | Stata: 0=Sun; R: 1=Sun by default |
| `quarter(date)` | `quarter(date)` | |
| `mofd(date)` | `floor_date(date, "month")` | |
| `date + 30` | `date + days(30)` | |
| `date + months(1)` | `date %m+% months(1)` | Calendar-aware |
| `date2 - date1` (days) | `as.numeric(date2 - date1)` | |
```stata
gen date = date(date_str, "YMD")
format date %td
gen yr = year(date)
```
```r
df <- df |> mutate(
date = ymd(date_str),
yr = year(date)
)
```
---
## Value Labels / Factors (forcats)
R's `factor()` is the direct equivalent of Stata's value label system. The
`forcats` package provides tools for manipulating factor levels.
| Stata | R | Notes |
|-------|---|-------|
| `label define lbl 1 "A" 2 "B"` + `label values var lbl` | `factor(var, levels = c(1,2), labels = c("A","B"))` | |
| `encode strvar, gen(numvar)` | `as.numeric(factor(strvar))` | |
| `decode numvar, gen(strvar)` | `as.character(numvar)` | If already factor |
| `label list lbl` | `levels(var)` | |
| `label values var .` (remove labels) | `as.numeric(as.character(var))` | Remove factor |
### forcats operations
```r
# Reorder levels by frequency (most common first)
df <- df |> mutate(category = fct_infreq(category))
# Lump infrequent levels into "Other"
df <- df |> mutate(category = fct_lump_n(category, n = 5))
# Reverse level order
df <- df |> mutate(category = fct_rev(category))
# Set reference level for regression
df <- df |> mutate(region = relevel(factor(region), ref = "Northeast"))
```
### Key Advantage Over Stata
In Stata, value labels are separate from the variable. In R, factors carry their
level information with them. This means:
- Factor ordering is preserved in plots (ggplot2 respects factor levels)
- Regression reference categories are controlled by factor levels
- No risk of label-variable mismatch
---
## Quick Reference Table
| Stata | R | Category |
|-------|---|----------|
| `substr(s, 1, 3)` | `str_sub(s, 1, 3)` | String |
| `strpos(s, "x")` | `str_detect(s, "x")` | String |
| `subinstr(s, "a", "b", .)` | `str_replace_all(s, "a", "b")` | String |
| `strtrim(s)` | `str_trim(s)` | String |
| `strlower(s)` | `str_to_lower(s)` | String |
| `regexm(s, "pat")` | `str_detect(s, "pat")` | String |
| `date("2024-03-15", "YMD")` | `ymd("2024-03-15")` | Date |
| `mdy(3, 15, 2024)` | `make_date(2024, 3, 15)` | Date |
| `year(date)` | `year(date)` | Date |
| `mofd(date)` | `floor_date(date, "month")` | Date |
| `date + 30` | `date + days(30)` | Date |
| `label define` + `label values` | `factor(var, levels, labels)` | Label |
| `encode strvar` | `as.numeric(factor(strvar))` | Label |
| `i.region` (in regression) | Factor auto-creates dummies | Categorical |
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |