###________ Section 1: Arithmetic and Assignment ________### 3*(2 + 1) g <- 4931 g g*1000 h <- c(15.1,11.3,10.0) h*2.7 length(h) ls() rm(g,h) ls() exp(1) log(10) log(10, base=10) ###________ Section 2: Descriptive Statistics ________### data(state) ls() state.x77 class(state.x77) x77.df <- as.data.frame(state.x77) class(x77.df) states <- state.abb murder <- x77.df[,5] #column 5 of the dataframe illit <- x77.df[,3] #same as state.x77[,3] rownames(x77.df) colnames(x77.df) names(x77.df) names(murder) names(murder) <- rownames(x77.df) murder names(murder) <- NULL #remove names murder <- x77.df$Murder #rate per 100,000 illit <- x77.df$Illiteracy #percent mean(murder) median(murder) ###________ Section 3: Comparison Operators ________### murder / illit abs(murder - mean(murder)) 2^c(5,8,3,1) 0:10 3:-7 murder > 12 which(murder == max(murder)) #index of the maximum value ###________ Section 4: Extracting Data; Subscripts ________### murder[1:3] murder[c(1,3,5)] # 1st, 3rd, and 5th values murder[murder > 12] states[murder > 12] illit[states == "CA"] murder murder[-1] murder[-7] ###________ Section 5: Basic Graphics ________### plot(illit,murder) abline(2.4, 4.3) identify(illit, murder, states, col = 2) #----- plot(illit,murder,type="n") text(illit,murder,states,col=2) #----- boxplot(x77.df$Population) hist(x77.df$Population) #----- summary(murder) vhigh <- murder >= 10.68 high <- murder >= 6.85 & murder < 10.68 low <- murder >= 4.35 & murder < 6.85 vlow <- murder < 4.35 plot(illit,murder,type = "n") points( illit[vhigh],murder[vhigh],pch="H", col=1 ) points( illit[high], murder[high], pch="h", col=2 ) text( illit[low], murder[low], "l", col=3 ) text( illit[vlow], murder[vlow], "L", col=4 ) #----- state.x <- state.center$x state.y <- state.center$y plot(state.x,state.y,type="n") text( state.x[vhigh],state.y[vhigh],state.abb[vhigh],col=1) text( state.x[high], state.y[high], state.abb[high], col=2) text( state.x[low], state.y[low], state.abb[low], col=3) text( state.x[vlow], state.y[vlow], state.abb[vlow], col=4) legend(-75,33, c("VHIGH","HIGH","LOW","VLOW"),pch=19,col=1:4,cex=.5) title(main = "Quartiles of murder rates by state", col = 6) ###________ Section 7: Matrices ________### y <- matrix(data=1, nr=3, nc=2) y y[,1] # column 1 of y y[2,] # row 2 of y t(y) # transpose of y t(y) %*% y # matrix multiplication y %*% t(y) y %*% y z <- matrix(nr=3, nc=2) z z <- matrix(data=1:6, nr=3, nc=2) z ###________ Section 8: Writing Functions ________### stdev <- function(x){ return(sqrt(var(x)))} stdev(murder) ###________ Section 9: CLT demonstration ________### x <- rchisq(2000,3) hist(x) y <- matrix(nr=64,nc=500) z <- vector(length=500) for(i in 1:500) y[,i] <- sample(x,64,replace=T) for(i in 1:500) z[i] <- mean( y[,i] ) z hist(z) mean(x) mean(z) sd(x) sd(z) sd(x)/sqrt(64)