Stata or R
[This article was first published on Enterprise Software Doesn't Have to Suck, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Recently I came across a complex model written in Access with complex SQL queries all over the place. The engineer who was maintaining it and I did some analysis and agreed that the model was using SQL in an unnatural way (things SQL isn’t good at) – complex logic, formatting etc.

What are the advantages of using Stata? Why shouldn’t I use R for this?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
To leave a comment for the author, please follow the link and comment on their blog: Enterprise Software Doesn't Have to Suck.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.