####################################################### ## calculator 2 + 3 4 * 2 / 13 + 7 result <- 2 + 3 print(result) ls() ####################################################### ## variables x <- 3 y <- 4 z <- x + y print(z) x <- c(1,2,3,4) sum(x) min(x) max(x) y <- c(5,6,7) z <- c(x,y) z <- 1:7 ####################################################### ## getting data in and out and dataframes load("state.Rda") ls() print(state) names(state) state$name state$unemp state$unifcont state2 <- read.csv("state.csv") ####################################################### ## descriptive statistics class.data <- c(1,3,4,4,5,10) mean(class.data) sd(class.data) median(class.data) quantile(class.data) IQR(class.data) mean(state$unemp) sd(state$unemp) median(state$unemp) quantile(state$unemp) IQR(state$unemp) ####################################################### ## histograms par(mar=c(5.1, 4.1, 0.1, 0.1)) hist(state$unemp, freq=FALSE, main="", xlab="Unemployment Rate") box() ####################################################### ## kernel density plots plot(density(state$unemp), main="", xlab="Unemployment Rate") ####################################################### ## histograms with statistical control par(mfrow=c(1,2), mar=c(5.1, 4.1, 0.1, 0.2)) hist(state$unemp[state$unifcont==1], freq=FALSE, main="", xlab="Unemployment Rate (Unified Control)", ylim=c(0,.45), xlim=c(2,9)) box() hist(state$unemp[state$unifcont==0], freq=FALSE, main="", xlab="Unemployment Rate (Divided Government)", ylim=c(0,.45), xlim=c(2,9)) box() ####################################################### ## boxplots par(mfrow=c(1,1), mar=c(0.1, 4.1, 0.1, 0.1)) boxplot(state$unemp, ylab="Unemployment Rate") ####################################################### ## frequency distribution and cross-tabulation load("nes1996.Rda") ls() table(nes1996$pid) table(nes1996$pid, nes1996$gender) ####################################################### ## Computing Normal Probabilities ## probability score between 73 to 87 pnorm(87, mean=80, sd=5) - pnorm(73, mean=80, sd=5) ## probability score between 71 to 86 pnorm(86, mean=80, sd=5) - pnorm(71, mean=80, sd=5) pnorm(1.2) - pnorm(-1.8)