Skip to content

Instantly share code, notes, and snippets.

@prasoonsharma
Created February 9, 2011 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save prasoonsharma/818724 to your computer and use it in GitHub Desktop.
Save prasoonsharma/818724 to your computer and use it in GitHub Desktop.
Stata and R
# 1) Stata command:
use growth_and_uptake_assumptions.dta
# Corresponding R command:
load(file="my_data.RData")
attach(my_data)
# ------------------------------------------------
# 2) Stata command:
sort year
# Corresponding R command:
my_order <- order(my_data$year)
# my_order will have the sorted index for "year"
# my_data[my_order, ] would show sorted values
# ------------------------------------------------
# 3) Stata command:
keep year *_trend
# Corresponding R command:
my_new_data <- my_data[, 1:4] # only keep the first 4 columns
# ------------------------------------------------
# 4) Stata command:
gen id = 1
# Corresponding R command:
id <- 1
# ------------------------------------------------
# 5) Stata command:
reshape wide *_trend, i(id) j(year)
# Corresponding R command:
attach(my_data)
install.packages("reshape")
library("reshape")
my_wide <- cast(my_data, wide + *_trend ~ id + year)
# ------------------------------------------------
# 6) Stata command:
merge category year using dumping_rate_append.dta
# Corresponding R command:
my_data2 <- merge(mydata,my_data1, by=c("category","year") ) # second data is loaded as my_data1
# ------------------------------------------------
# 7) Stata command:
replace rate = 0 if category== "D" | category == "E"
# Corresponding R command:
rate[category=="D" |category == "E"] = 0
# ------------------------------------------------
# 8) Stata command:
insheet using $dirprm/prmBasetrend_ByCvT_ByYear.txt, tab
# Corresponding R command:
my_data <- read.csv(file="simple.csv",head=TRUE,sep="\t")
# ------------------------------------------------
# 9) Stata command:
clear
# Corresponding R command:
rm(list = ls())
# ------------------------------------------------
# 10) Stata command:
??
# Corresponding R command:
memory.limit(size=3000) # SET MEMORY TO USE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment