Basics
Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.
cd
Change the working directory
Stata
cd "path/to/folder"R Package:base
setwd("path/to/folder")An RStudio project sets the working directory for you, so scripts can use relative paths.
clear
Remove a dataset from memory
Stata
clearR Package:base
rm(data)Rarely needed: R holds many tables at once, each under its own name.
dir
List the files in the working directory
Stata
dirR Package:base
dir()display
do
Run a script file
Stata
do analysis.doR Package:base
source("analysis.R")export
Export a CSV file
Stata
export delimited data.csvR Package:readr
readr::write_csv(data, "data.csv")1 more examples and alternatives
Export an Excel file
Stata
export excel data.xlsxR Package:writexl
writexl::write_xlsx(data, "data.xlsx")findit
Find the R equivalent of a Stata command
Stata
findit commandR Package:tidystata
findit(command)Bare words and comma-separated names work: findit(tab, sex, region). Add hints such as findit(tab, gen), or quote full Stata syntax. One clear example is shown by default; print(n = Inf) reveals every match. findit() with no argument returns the whole list.
global
Store a value for later use
Stata
global path "path/to/folder"R Package:base
path <- "path/to/folder"Any R object works like a macro: assign it once and reuse its name, as in setwd(path).
help
Open the help page of a function
Stata
help commandR Package:utils
help(function_name)?function does the same. In RStudio you can also search the Help pane.
import
Import a CSV file
Stata
import delimited data.csvR Package:readr
data <- readr::read_csv("data.csv")RStudio’s File > Import Dataset menu writes this code for you.
Import an Excel sheet
Stata
import excel data.xlsx, firstrowR Package:readxl
data <- readxl::read_excel("data.xlsx")Import a SAS file
Stata
import sas data.sas7bdatR Package:haven
data <- haven::read_sas("data.sas7bdat")Import an SPSS file
Stata
import spss data.savR Package:haven
data <- haven::read_sav("data.sav")log
Record console output in a file
Stata
log using results.logR Package:base
sink("results.log")Most R users write an R Markdown or Quarto report instead of a log.
net
Install a package from GitHub
Stata
net install package, from(url)R Package:remotes
remotes::install_github("user/package")pwd
save
Save as a Stata dataset
Stata
save data.dta, replaceR Package:haven
haven::write_dta(data, "data.dta")saveRDS(data, “data.rds”) keeps every R type, such as factors and dates, exactly.
saveold
Save for an older Stata version
Stata
saveold data.dta, version(13)R Package:haven
haven::write_dta(data, "data.dta", version = 13)search
Search all installed help pages
Stata
search keywordR Package:utils
help.search("keyword")??keyword is a shortcut. To find the R version of a Stata command, use findit().
ssc
Install a package
Stata
ssc install packageR Package:utils
install.packages("package")Unlike Stata, an installed package must be attached in every session with library(package).
sysuse
Load an example dataset
Stata
sysuse autoR Package:datasets
data("mtcars")use
Open a Stata dataset
Stata
use data.dtaR Package:haven
data <- haven::read_dta("data.dta")Variable and value labels are kept. Data saved in R’s own format open with readRDS(“data.rds”) or load(“data.RData”).
webuse
Load an example dataset from the Stata website
Stata
webuse nlsworkR Package:haven
data <- haven::read_dta("https://www.stata-press.com/data/r18/nlswork.dta")