# 1 - pag. 567-568 tbl <- c(268,41,32,32,28,120,41,20,12,121) tbl ptbl <- prop.table(tbl) # the same can be obtained with ptbl <- tbl/sum(tbl) ptbl region <- c("Florida","Hawaii","California","Sud Carolina","Nord Carolina","Australia","Sud Africa","Brasile","Bahamas","Altri") data.frame(Paesi= region, Frequenze = tbl, Proporzioni = round(ptbl,3), Percentuali = round(ptbl*100,1)) # ------------------------------------------ # Table 2.4 Frequency Table for Sodium in 20 Breakfast Cereals. #data x <- c(0,340,70,140,200,180,210,150,100,130,140,180,190,160,290,50,220,180,200,210) # equal intervals (breaks) brk <- c(0,40,80,120,160,200,240,280,320,360) #classify obs. within intervals xx <- cut(x,brk,right=FALSE) #Count number of observations on each interval tbl <- table(xx) #create book’s Table 2.4 ptbl <- prop.table(tbl) Tab2.4<- data.frame( Intervalli=names(tbl), Frequenze=as.numeric(tbl), Proporzioni=round(as.numeric(ptbl),3), Percentuali=round(as.numeric(ptbl)*100,1) ) Tab2.4 barplot(tbl,# Frequency data space = 0,# Space between bars xlab="Sodium (mg)",# X axis label ylab="Frequency")# Y axis label # ------------------------------------------ # straightforward function hist(x) #use the built-in hist() function hist(x, breaks = c(0,39,79,119,159,199,239,279,319,359), right = TRUE, main = NULL, xlab = "Sodium (mg)", freq = TRUE, xaxt = "n"# remove x-axis tickmarks ) #Put them back in the right place... axis(side = 1,at = c(0,40,80,120,160,200,240,280,320,360)) # ------------------------------------------ #removing breaks hist(x, #breaks = c(0,39,79,119,159,199,239,279,319,359), right = TRUE, main = NULL, xlab = "Sodium (mg)", freq = TRUE, #xaxt = "n"# remove x-axis tickmarks ) # ------------------------------------------ # setting freq = FALSE #use the built-in hist() function hist(x, breaks = c(0,39,79,119,159,199,239,279,319,359), right = TRUE, main = NULL, xlab = "Sodium (mg)", freq = FALSE, xaxt = "n"# remove x-axis tickmarks ) #Put them back in the right place... axis(side = 1,at = c(0,40,80,120,160,200,240,280,320,360)) # ------------------------------------------ # density computation #class amplitude brk diff(brk) #class proportion ptbl # density = class relative frequency/class amplitude density = ptbl/40 density # ------------------------------------------ #Time Plots #install package from local file (.zip or tar.gz) # For Windows users install.packages("C://Users//Grassi//Downloads//sasld_1.0.tar.gz",type = "source") # For Mac a typical path could be: "~/../.../", # install.packages("~/Downloads/sasld_1.0.tar",type = "source",repos = NULL) # important set repos=NULL; getOption("repos") # 3 - pag. 569 #load ny data set library(sasld) data(ny) plot(x = ny$YEAR, y = ny$ANNUAL, type = "l", pch = 19, xlab = "Anno", ylab = "Temperatura Media Annuale" ) lines( lowess(ny$ANNUAL ~ ny$YEAR), lwd=2) # Change y limits plot(x = ny$YEAR, y = ny$ANNUAL, type = "l", pch = 19, xlab = "Anno", ylab = "Temperatura Media Annuale", ylim = c(20,90)) #and add AUG - APR and FEB points(ny$YEAR,ny$AUG,type="l",col="red") points(ny$YEAR,ny$APR,type="l",col="orange") points(ny$YEAR,ny$FEB,type="l",col="blue") legend(x=1980,y=70, legend=c("FEB","APR","AUG"), text.col=c("blue","orange","red"))