R version 2.10.1 (2009-12-14) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [R.app GUI 1.31 (5537) x86_64-apple-darwin9.8.0] > rbind(c(56,8),c(2,16)) [,1] [,2] [1,] 56 8 [2,] 2 16 > rbind(c(56,8),c(2,16),c(1,2)) [,1] [,2] [1,] 56 8 [2,] 2 16 [3,] 1 2 > cbind(c(56,8),c(2,16),c(1,2)) [,1] [,2] [,3] [1,] 56 2 1 [2,] 8 16 2 > cbind(c(56,8),c(2,16),c(1,2))[,2] [1] 2 16 > cbind(c(56,8),c(2,16),c(1,2))[1,2] [1] 2 > matrix(c(56,2,8,16),nrow=2) [,1] [,2] [1,] 56 8 [2,] 2 16 > matrix(c(56,2,8,16),nrow=1) [,1] [,2] [,3] [,4] [1,] 56 2 8 16 > matrix(c(56,2,8,16),nrow=4) [,1] [1,] 56 [2,] 2 [3,] 8 [4,] 16 > matrix(c(56,2,8,16),nrow=3) [,1] [,2] [1,] 56 16 [2,] 2 56 [3,] 8 2 Warning message: In matrix(c(56, 2, 8, 16), nrow = 3) : data length [4] is not a sub-multiple or multiple of the number of rows [3] > matrix(c(56,2,8,16),nrow=2) [,1] [,2] [1,] 56 8 [2,] 2 16 > t(matrix(c(56,2,8,16),nrow=2)) [,1] [,2] [1,] 56 2 [2,] 8 16 > library(UsingR) > table(grades) grade prev A A- B+ B B- C+ A 15 3 1 4 0 0 A- 3 1 1 0 0 0 B+ 0 2 2 1 2 0 B 0 1 1 4 3 1 B- 0 1 0 2 0 0 C+ 1 1 0 0 0 0 C 1 0 0 1 1 3 D 0 0 0 1 0 0 F 1 0 0 1 1 1 grade prev C D F A 3 2 0 A- 0 0 0 B+ 0 1 1 B 3 0 2 B- 1 0 0 C+ 1 0 0 C 5 9 7 D 4 3 1 F 3 4 11 > t(table(grades)) prev grade A A- B+ B B- C+ A 15 3 0 0 0 1 A- 3 1 2 1 1 1 B+ 1 1 2 1 0 0 B 4 0 1 4 2 0 B- 0 0 2 3 0 0 C+ 0 0 0 1 0 0 C 3 0 0 3 1 1 D 2 0 1 0 0 0 F 0 0 1 2 0 0 prev grade C D F A 1 0 1 A- 0 0 0 B+ 0 0 0 B 1 1 1 B- 1 0 1 C+ 3 0 1 C 5 4 3 D 9 3 4 F 7 1 11 > x=t(table(grades)) > fix(x) Warning: class(es) of 'name' will be discarded > x A A- B+ B B- C+ A 13 3 0 0 0 1 A- 3 1 2 1 1 1 B+ 1 1 2 1 0 0 B 4 0 1 4 2 0 B- 0 0 2 3 0 0 C+ 0 0 0 1 0 0 C 3 0 0 3 1 1 D 2 0 1 0 0 0 F 0 0 1 2 0 0 C D F A 1 0 1 A- 0 0 0 B+ 0 0 0 B 1 1 1 B- 1 0 1 C+ 3 0 1 C 5 4 3 D 9 3 4 F 7 1 11 > x=matrix(c(56,2,8,16),nrow=2) > x [,1] [,2] [1,] 56 8 [2,] 2 16 > addmargins(x) [,1] [,2] [,3] [1,] 56 8 64 [2,] 2 16 18 [3,] 58 24 82 > apply(x,1,sum) [1] 64 18 > apply(x,2,sum) [1] 58 24 > margin.table(x,1) [1] 64 18 > sum(table(x)) [1] 4 > x [,1] [,2] [1,] 56 8 [2,] 2 16 > sum(x) [1] 82 > x [,1] [,2] [1,] 56 8 [2,] 2 16 > x[1,1]/sum(x) [1] 0.6829268 > 56/82 [1] 0.6829268 > x [,1] [,2] [1,] 56 8 [2,] 2 16 > prop.table(x) [,1] [,2] [1,] 0.68292683 0.09756098 [2,] 0.02439024 0.19512195 > prop.table(x)[1,1] [1] 0.6829268 > sum(prop.table(x)) [1] 1 > x [,1] [,2] [1,] 56 8 [2,] 2 16 > table(x) x 2 8 16 56 1 1 1 1 > x [,1] [,2] [1,] 56 8 [2,] 2 16 > rownames(x)=c('Y','not-Y') > x [,1] [,2] Y 56 8 not-Y 2 16 > rownames(x) [1] "Y" "not-Y" > prob.yz=prop.table(x) > colnames(x)=c('Z','not-Z') > prob.yz=prop.table(x) > prob.yz Z not-Z Y 0.68292683 0.09756098 not-Y 0.02439024 0.19512195 > margin.table(prob.yz) [1] 1 > margin.table(prob.yz,1) Y not-Y 0.7804878 0.2195122 > margin.table(prob.yz,2) Z not-Z 0.7073171 0.2926829 > prob.yz[1,1]+prob.yz[1,2] [1] 0.7804878 > prob.yz[2,1]+prob.yz[2,2] [1] 0.2195122 > pz.given.y=prob.yz[1,1]/(prob.yz[1,1]+prob.yz[1,2]) > pz.given.y [1] 0.875 > prob.yz[1,2]/(prob.yz[1,1]+prob.yz[1,2]) [1] 0.125 > prob.yz[1,]/(prob.yz[1,1]+prob.yz[1,2]) Z not-Z 0.875 0.125 > prob.yz[1,]/sum(prob.yz[1,]) Z not-Z 0.875 0.125 > prob.yz[,1]/sum(prob.yz[,1]) Y not-Y 0.96551724 0.03448276 > prob.yz Z not-Z Y 0.68292683 0.09756098 not-Y 0.02439024 0.19512195 > prob.yz[,2]/sum(prob.yz[,2]) Y not-Y 0.3333333 0.6666667 > table(grades$prev,grades$grade) A A- B+ B B- C+ A 15 3 1 4 0 0 A- 3 1 1 0 0 0 B+ 0 2 2 1 2 0 B 0 1 1 4 3 1 B- 0 1 0 2 0 0 C+ 1 1 0 0 0 0 C 1 0 0 1 1 3 D 0 0 0 1 0 0 F 1 0 0 1 1 1 C D F A 3 2 0 A- 0 0 0 B+ 0 1 1 B 3 0 2 B- 1 0 0 C+ 1 0 0 C 5 9 7 D 4 3 1 F 3 4 11 > x Z not-Z Y 56 8 not-Y 2 16 > prop.table(x) Z not-Z Y 0.68292683 0.09756098 not-Y 0.02439024 0.19512195 > barplot(prop.table(x),legend.text=TRUE,ylim=0:1) > barplot(prop.table(table(grades)),legend.text=TRUE,ylim=c(0,0.2)) > barplot(prop.table(table(grades)),legend.text=TRUE,ylim=c(0,0.2),beside=TRUE) > library(MASS) > geyser waiting duration 1 80 4.0166667 2 71 2.1500000 3 57 4.0000000 4 80 4.0000000 5 75 4.0000000 6 77 2.0000000 7 60 4.3833333 8 86 4.2833333 9 77 2.0333333 10 56 4.8333333 ... > fivenum(geyser) [1] 0.8333333 4.0000000 24.2250000 [4] 76.0000000 108.0000000 > boxplot(geyser$waiting,geyser$duration,names=colnames(geyser),main='geyser data') > fivenum(geyser$duration) [1] 0.8333333 2.0000000 4.0000000 [4] 4.3833333 5.4500000 > fivenum(geyser$waiting) [1] 43 59 76 83 108 > boxplot(geyser$waiting/max(geyser$waiting),geyser$duration/max(geyser$duration),names=colnames(geyser)) > y=geyser$waiting/max(geyser$waiting)*100 > y=geyser$waiting/max(geyser$waiting)*100 > plot(density(y)) > z=geyser$duration/max(geyser$duration)*100 > lines(density(z),lty=2) > plot(geyser$waiting,geyser$duration,col=c('red','blue'),pch=19) > ?homedata starting httpd help server ... done > plot(homedata) > plot(homedata,col=c('red','blue'),pch=19) > qqplot(homedata$y1970,homedata$y2000) > qqplot(homedata$y1970,homedata$y2000) > qqplot(geyser$waiting,geyser$duration) > qqplot(geyser$waiting,homedata$y2000) # valid correlation? > cor(homedata$y1970,homedata$y2000) [1] 0.8962155