### Introduction to R ### # 1. R is a Calculator 2 + 2 exp(-2) log(1) rnorm(10) rnorm(10,mean=2,sd=0.5) # 2. Assignment x <- 2 x x+x x*x # 3. Vector weight <- c(65,45,67,78,56) weight height <- c(1.7,1.8,1.76,1.65,1.74) height length(weight) length(height) bmi <- weight/height^2 bmi sum(weight) xbar <- sum(weight)/length(weight) mean(weight) # 3. Plots plot(height,weight) plot(height,weight,pch=2) abline(a=45,b=10) # 4. Matrix and Array x <- 1:12 x dim(x) <- c(3,4) x matrix(1:12,nrow=3,byrow=T) matrix(1:12,nrow=3,byrow=F) rownames(x) <- LETTERS[1:3] x t(x) cbind(weight,height) rbind(weight,height) # 5. Indexing x[,1] x[1,] x[,1:2] x[,c(2,4)] weight[height>1.7] # 6. Data entry US90<-read.table("US90.txt", header=T) US90 US90$year US90$gdpgr attach(US90) year detach(US90) # 7. US economy in 90s US90<-read.table("US90.txt", header=T) attach(US90) # D <-read.csv("giffen.csv", header=T) summary(US90) summary(gdpgr) mean(inf) sd(inf) var(inf) # 8. Regression lm(gdpgr~invgr) summary(lm(gdpgr~invgr)) lm(gdpgr~ 0 + invgr) lm(log(gdpgr)~log(consgr)+log(invgr)+log(producgr)) a <- lm(log(gdpgr)~log(consgr)+log(invgr)+log(producgr)) coef(a) fitted(a) resid(a) vcov(a)