Skip to content

Instantly share code, notes, and snippets.

@rblissett
Last active January 24, 2017 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rblissett/2ea47d69878a800fab1f22036c958f85 to your computer and use it in GitHub Desktop.
Save rblissett/2ea47d69878a800fab1f22036c958f85 to your computer and use it in GitHub Desktop.
Code to accompany RTutorial_160930.pdf
## RTutorial.R
## Richard Blissett (rsl.bliss@gmail.com)
## Code to accompany RTutorial_160930.pdf
###########################################################
# Set up environment
###########################################################
# Clear environment
rm(list=ls())
# Load packages
library(haven)
# Set working directory
setwd("~/Dropbox/Teaching/RTutorial")
# Read in data
api <- read_dta("apidata_2012_so.dta")
###########################################################
# Accessing and manipulating variables
###########################################################
# See list of variable names
names(api)
# Get summary statistics
summary(api$cst28_engl)
# Create new variable
api$total <- api$cst28_engl + api$cst28_math
# Show new list of variable names
names(api)
# Drop a variable
api$total <- NULL
# Show new list of variable names
names(api)
###########################################################
# Data analysis
###########################################################
# Store summary information in an object
msum <- summary(api$cst28_engl)
# Show full summary
msum
# Get first item in list by index
msum[1]
# Get first item in list by name
msum["Min."]
# Show frequency for categorical variable
table(api$districttype)
# Run the model
model1 <- lm(cst28_engl ~ cst28_math + pctfrpl, data=api)
# See abbreviated results
model1
# See more detailed results
summary(model1)
# See attributes of model1 object
attributes(model1)
# See attributes of summary of model1 object
attributes(summary(model1))
# Pull out just coefficient information
summary(model1)$coefficients
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment