#Solutions to Homework 2 attach(USArrests) ##compute the mean and variance for all of the variables means<-apply(USArrests,2,mean) vars<-apply(USArrests,2,var) ##get a subset of the dataset arrests_new<-USArrests[,c(1,2,4)] ##compute the total number of crimes for each state using the apply statement total<-apply(arrests_new,1,sum) ##attach this to the arrests_new dataset arrests_new<-cbind(arrests_new,total) ##compute quintiles of total pvec<-c(0.2,0.4,0.6,0.8,1) quantile(arrests_new\$total,pvec) ##create a dot plot of the total number of crimes and save it as a png file png("dotplot_total.png") dotchart(arrests_new\$total, xlab="Total Number of Crimes", main = "Total Number of Crimes in all 50 States per 100,000",col="purple") dev.off() ##use the swiss dataset to produce plots attach(swiss) #png("histograms_of_swiss_variables.png") par(mfrow=c(2,3)) hist(Fertility,xlab="Fertility Ig") hist(Agriculture,xlab="% of Males in Agriculture") hist(Examination,xlab="% of Draftees Receiving Highest Mark") hist(Education,xlab="% of Draftees Educated Beyond Primary School") hist(Catholic, xlab="% Catholic") hist(Infant.Mortality, xlab="% of Live Births That Live Less Than 1 Year") dev.off() ##scatterplots vs. Infant Mortality png("Scatterplots_Swiss.png") par(mfrow=c(5,1)) plot(Fertility,Infant.Mortality,xlab="Fertility",ylab="Infant Mortality") plot(Agriculture,Infant.Mortality,xlab="Agriculture",ylab="Infant Mortality") plot(Examination,Infant.Mortality,xlab="Examination",ylab="Infant Mortality") plot(Education,Infant.Mortality,xlab="Education",ylab="Infant Mortality") plot(Catholic,Infant.Mortality,xlab="Catholic",ylab="Infant Mortality") dev.off() ##table attach(CO2) ftable(Type,Treatment)