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

clear

R 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

dir

R Package:base

dir()

display

Calculate and show a result

Stata

display 2 + 2

R Package:base

2 + 2

do

Run a script file

Stata

do analysis.do

R Package:base

source("analysis.R")

export

Export a CSV file

Stata

export delimited data.csv

R Package:readr

readr::write_csv(data, "data.csv")
1 more examples and alternatives

Export an Excel file

Stata

export excel data.xlsx

R Package:writexl

writexl::write_xlsx(data, "data.xlsx")

findit

Find the R equivalent of a Stata command

Stata

findit command

R 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 command

R 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.csv

R 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, firstrow

R Package:readxl

data <- readxl::read_excel("data.xlsx")

Import a SAS file

Stata

import sas data.sas7bdat

R Package:haven

data <- haven::read_sas("data.sas7bdat")

Import an SPSS file

Stata

import spss data.sav

R Package:haven

data <- haven::read_sav("data.sav")

log

Record console output in a file

Stata

log using results.log

R 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

Show the working directory

Stata

pwd

R Package:base

getwd()

save

Save as a Stata dataset

Stata

save data.dta, replace

R 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)

ssc

Install a package

Stata

ssc install package

R 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 auto

R Package:datasets

data("mtcars")

use

Open a Stata dataset

Stata

use data.dta

R 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 nlswork

R Package:haven

data <- haven::read_dta("https://www.stata-press.com/data/r18/nlswork.dta")