├── Lecture1.R ├── Lecture2.R ├── Lecture3.R ├── Lecture4.R ├── Lecture5.R ├── Lecture6.R └── README.md /Lecture1.R: -------------------------------------------------------------------------------- 1 | install.packages('ggplot2') 2 | library(ggplot2) 3 | update.packages() 4 | 5 | v = c(1,4,4,3,2,2,3) 6 | v[c(2,3,4)] 7 | v[2:4] 8 | v[c(2,4,3)] 9 | v[-2] 10 | v[-2:-4] 11 | v[v<3] 12 | which(v==3) 13 | 14 | set.seed(250) 15 | a = runif(3, min=0, max=100) 16 | floor(a) 17 | ceiling(a) 18 | round(a,4) 19 | 20 | data1=read.csv(file="~/documents/rugby.txt") 21 | data2=read.table(file="~/documents/rugby.txt") 22 | data3=read.csv("http://www.macalester.edu/~kaplan/ISM/datasets/swim100m.csv") 23 | attach(data3) 24 | 25 | set.seed(123) 26 | x=rnorm(100,mean=100,sd=10) 27 | set.seed(234) 28 | y=rnorm(100,mean=100,sd=10) 29 | hist(x,breaks=20) 30 | plot(density(x)) 31 | plot(x) 32 | boxplot(x,y) 33 | boxplot(time~sex) 34 | qqnorm(x) 35 | qqline(x) 36 | qqplot(x,y) 37 | -------------------------------------------------------------------------------- /Lecture2.R: -------------------------------------------------------------------------------- 1 | a = c(1, 2, 5, 3, 6, -2, 4) 2 | b = c("one", "two", "three") 3 | c = c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE) 4 | 5 | x = matrix(1:20, nrow=5, ncol=4, byrow=TRUE) 6 | x 7 | y = matrix(1:20, nrow=5, ncol=4, byrow=FALSE) 8 | y 9 | x[2,] 10 | x[,2] 11 | x[1,4] 12 | x[2,c(2,4)] 13 | x[3:5, 2] 14 | rnames=c("apple","banana","orange","melon","corn") 15 | cnames=c("cat","dog","bird","pig") 16 | x = matrix(1:20, nrow=5, ncol=4, byrow=TRUE) 17 | rownames(x)=rnames 18 | colnames(x)=cnames 19 | 20 | dim1 = c("A1", "A2") 21 | dim2 = c("B1", "B2", "B3") 22 | dim3 = c("C1", "C2", "C3", "C4") 23 | dim4 = c("D1", "D2", "D3") 24 | z = array(1:72, c(2, 3, 4, 3), dimnames=list(dim1, dim2, dim3, dim4)) 25 | z 26 | z[1,2,3,] 27 | 28 | patientID = c(1, 2, 3, 4) 29 | age = c(25, 34, 28, 52) 30 | diabetes = c("Type1", "Type2", "Type1", "Type1") 31 | status = c("Poor", "Improved", "Excellent", "Poor") 32 | patientdata = data.frame(patientID, age, diabetes, status) 33 | patientdata 34 | swim = read.csv("http://www.macalester.edu/~kaplan/ISM/datasets/swim100m.csv") 35 | patientdata[1:2] 36 | patientdata[1:3] 37 | patientdata[1,1:3] 38 | patientdata[c(1,3),1:3] 39 | 40 | mylist = list(patientdata, swim, x) 41 | mylist 42 | 43 | par(mfrow=c(2,2)) 44 | plot(rnorm(50),pch=17) 45 | plot(rnorm(20),type="l",lty=5) 46 | plot(rnorm(100),cex=0.5) 47 | plot(rnorm(200),lwd=2) 48 | 49 | attach(mtcars) 50 | layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE)) 51 | hist(wt) 52 | hist(mpg) 53 | hist(disp) 54 | detach(mtcars) 55 | 56 | -------------------------------------------------------------------------------- /Lecture3.R: -------------------------------------------------------------------------------- 1 | x = matrix(1:20, nrow=5, ncol=4, byrow=TRUE) 2 | swim = read.csv("http://www.macalester.edu/~kaplan/ISM/datasets/swim100m.csv") 3 | mylist=list(swim,x) 4 | mylist[[2]][2] 5 | mylist[[2]][1:2] 6 | mylist[[2]][1:2,3] 7 | mylist[[2]][1:2,] 8 | 9 | for(i in 1:10){ 10 | print(i) 11 | } 12 | i = 1 13 | while(i <= 10){ 14 | print(i) 15 | i=i+1 16 | } 17 | 18 | i = 1 19 | if(i == 1){ 20 | print("Hello World") 21 | } 22 | 23 | i = 2 24 | if(i == 1){ 25 | print("Hello World!") 26 | }else{ 27 | print("Goodbye World!") 28 | } 29 | 30 | feelings = c("sad", "afraid") 31 | for (i in feelings){ 32 | print( 33 | switch(i, 34 | happy = "I am glad you are happy", 35 | afraid = "There is nothing to fear", 36 | sad = "Cheer up", 37 | angry = "Calm down now" 38 | ) 39 | ) 40 | } 41 | 42 | myfunction = function(x,a,b,c){ 43 | return(a*sin(x)^2 - b*x + c) 44 | } 45 | curve(myfunction(x,20,3,4),xlim=c(1,20)) 46 | 47 | myfeeling = function(x){ 48 | for (i in x){ 49 | print( 50 | switch(i, 51 | happy = "I am glad you are happy", 52 | afraid = "There is nothing to fear", 53 | sad = "Cheer up", 54 | angry = "Calm down now" 55 | ) 56 | ) 57 | } 58 | } 59 | feelings = c("sad", "afraid") 60 | myfeeling(feelings) 61 | -------------------------------------------------------------------------------- /Lecture4.R: -------------------------------------------------------------------------------- 1 | #Bar Chart 2 | library(vcd) 3 | counts = table(Arthritis$Improved) 4 | counts 5 | par(mfrow=c(2,2)) 6 | barplot(counts, 7 | main="Simple Bar Plot", 8 | xlab="Improvement", ylab="Frequency") 9 | 10 | barplot(counts, 11 | main="Horizontal Bar Plot", 12 | xlab="Frequency", ylab="Improvement", 13 | horiz=TRUE) 14 | 15 | counts <- table(Arthritis$Improved, Arthritis$Treatment) 16 | counts 17 | 18 | barplot(counts, 19 | main="Stacked Bar Plot", 20 | xlab="Treatment", ylab="Frequency", 21 | col=c("red", "yellow","green"), 22 | legend=rownames(counts)) 23 | 24 | barplot(counts, 25 | main="Grouped Bar Plot", 26 | xlab="Treatment", ylab="Frequency", 27 | col=c("red", "yellow", "green"), 28 | legend=rownames(counts), beside=TRUE) 29 | 30 | #Pie Chart 31 | library(plotrix) 32 | par(mfrow=c(2,2)) 33 | slices <- c(10, 12,4, 16, 8) 34 | lbls <- c("US", "UK", "Australia", "Germany", "France") 35 | pie(slices, labels = lbls,main="Simple Pie Chart",edges=300,radius=1) 36 | pct <- round(slices/sum(slices)*100) 37 | lbls2 <- paste(lbls, " ", pct, "%", sep="") 38 | pie(slices, labels=lbls2, col=rainbow(length(lbls2)), 39 | main="Pie Chart with Percentages",edges=300,radius=1) 40 | pie3D(slices, labels=lbls,explode=0.5, 41 | main="3D Pie Chart ",edges=300,radius=1) 42 | mytable <- table(state.region) 43 | lbls3 <- paste(names(mytable), "\n", mytable, sep="") 44 | pie(mytable,labels=lbls3, 45 | main="Pie Chart from a Table\n(with sample size)", 46 | edges=300,radius=1) 47 | #Fan Plot 48 | slices <- c(10, 12,4, 16, 8) 49 | lbls <- c("US", "UK", "Australia", "Germany", "France") 50 | fan.plot(slices, labels = lbls, main="Fan Plot") 51 | 52 | #Dot Chart 53 | dotchart(mtcars$mpg, 54 | labels=row.names(mtcars),cex=0.7, 55 | main="Gas Mileage for Car Models", 56 | xlab="Miles Per Gallon") 57 | 58 | #summary 59 | head(mtcars) 60 | summary(mtcars) 61 | 62 | #Tables 63 | attach(mtcars) 64 | table(cyl) 65 | summary(mpg) 66 | table(cut(mpg,seq(10,34,by=2))) 67 | 68 | #Correlations 69 | states = state.x77[,1:6] 70 | cov(states) 71 | var(states) 72 | cor(states) 73 | 74 | #T test 75 | x = rnorm(100, mean = 10, sd = 1) 76 | y = rnorm(100, mean = 30, sd = 10) 77 | t.test(x, y, alt = "two.sided",paired=TRUE) 78 | 79 | #Wilcoxon 80 | wilcox.test(x,y,alt="less") 81 | 82 | 83 | #Practical Example 84 | data=read.csv("~/documents/R Programming/project/STAT.csv") 85 | attach(data) 86 | layout(matrix(c(1,1,2,3),2,2,byrow=TRUE)) 87 | hist(difference,col="blue") 88 | hist(Target,col="blue") 89 | hist(Walmart,col="blue") 90 | 91 | par(mfrow=c(2,1)) 92 | boxplot(data[2:4],horizontal=TRUE) 93 | boxplot(difference,horizontal=TRUE) 94 | 95 | par(mfrow=c(1,1)) 96 | qqnorm(difference) 97 | qqline(difference) 98 | t.test(difference,alter="two.sided") 99 | binom.test(length(difference[difference>=0]), 100 | length(difference), 101 | alter="two.sided") 102 | wilcox.test(difference,alter="two.sided") 103 | 104 | library(nortest) 105 | ad.test(difference) 106 | -------------------------------------------------------------------------------- /Lecture5.R: -------------------------------------------------------------------------------- 1 | #R for MATLAB users: 2 | #http://mathesaurus.sourceforge.net/octave-r.html 3 | set.seed(123) 4 | A = matrix(sample(100,15), nrow=5, ncol=3) 5 | set.seed(234) 6 | B = matrix(sample(100,15), nrow=5, ncol=3) 7 | set.seed(321) 8 | X = matrix(sample(100,25), nrow=5, ncol=5) 9 | set.seed(213) 10 | b = matrix(sample(100,5),nrow=5, ncol=1) 11 | A 12 | B 13 | X 14 | b 15 | # + - * / ^ 16 | #Element-wise addition, subtraction, multiplication, division 17 | #, and exponentiation, respectively. 18 | A + 2 19 | A * 2 20 | A ^ 2 21 | 22 | #Matrix multiplication 23 | t(A) %*% B 24 | 25 | #Returns a vector containing the column means of A. 26 | colMeans(A) 27 | 28 | #Returns a vector containing the column sums of A. 29 | colSums(A) 30 | 31 | #Returns a vector containing the row means of A. 32 | rowMeans(A) 33 | 34 | #Returns a vector containing the row sums of A. 35 | rowSums(A) 36 | 37 | 38 | #Matrix Crossproduct 39 | # A'A 40 | crossprod(A) 41 | # A'B 42 | crossprod(A,B) 43 | 44 | #Inverse of A where A is a square matrix. 45 | solve(X) 46 | 47 | #Solves for vector x in the equation b = Ax. 48 | # b = Xv 49 | v = solve(X, b) 50 | v 51 | #Returns a vector containing the elements of the principal diagonal 52 | diag(X) 53 | 54 | #Creates a diagonal matrix with the elements of x in the principal diagonal. 55 | diag(c(1,2,3,4)) 56 | 57 | #If k is a scalar, this creates a k x k identity matrix. 58 | diag(5) 59 | 60 | #Eigenvalues and eigenvectors of A. 61 | eigen(X) 62 | -------------------------------------------------------------------------------- /Lecture6.R: -------------------------------------------------------------------------------- 1 | # FACTOR 2 | factor = factor(rep(c(1:3),times=5)) 3 | factor 4 | X = sample(100,15) 5 | ?tapply 6 | tapply(X,factor,mean) 7 | rbind(X,factor) 8 | boo = rbind(X,factor)[2,]==1 9 | which(boo) 10 | rbind(X,factor)[1,which(boo)] 11 | sum(rbind(X,factor)[1,which(boo)])/length(which(boo)) 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IntroToR 2 | R Codes for Introduction to R Programming lectures 3 | 4 | Feel free to download, play with, and redistribute these files. 5 | 6 | I would be very happy if people can learn R with the help of my code. 7 | 8 | The video lectures (in mandarin) are available at https://www.youtube.com/playlist?list=PLBTcf4SwWEI9_kCOJ-1o-Jwr-_Qb6bkeg 9 | 10 | Contact me (minghaowu_2015@163.com) if there is any problem, but reply is not guaranteed. 11 | 12 | --------------------------------------------------------------------------------