7. 插入 R Code Chuncks:
作業一 (A Short Introduction to R)
ToDo 1
(2014 - 2009) / (2014 - 1991) * 100
## [1] 21.73913
ToDo 2
a = (2014 - 2009) / (2014 - 1991) * 100
a
## [1] 21.73913
ToDo 3
b = c(4,5,8,11)
sum(b)
## [1] 28
x = rnorm(100)
plot(x)
ToDo 4: Help and documentation
help(sqrt)
## starting httpd help server ... done
ToDo 5: Scripts
setwd("D:/R_Labs/rmd_demo")
source("firstscript.R")
ToDo 6: Data structures
p = 31:60
Q = matrix(data=p, nrow = 6)
x1 = rnorm(100)
x2 = rnorm(100)
x3 = rnorm(100)
t = data.frame(a=x1,b=x1+x2,c=x1+x2+x3)
plot(t)
sd(as.numeric(unlist(t)))
## [1] 1.245676
ToDo 7: Graphic
plot(t$a, type="l", ylim=range(t),lwd=3, col=rgb(1,1,0,0.7))
lines(t$b, type="s", lwd=2,col=rgb(0.3,0.4,0.3,0.9))
points(t$c, pch=22, cex=2,col=rgb(0,0,1,0.3))
ToDo 8: Reading and writing data files
setwd("D:/R_Labs/rmd_demo")
d = read.table(file="tst1.txt",header = TRUE)
d$g = d$g * 5
write.table(d,file="tst2.txt",row.names=FALSE)
ToDo 9: Not available data
mean(sqrt(rnorm(100)))
## Warning in sqrt(rnorm(100)): 產生了 NaNs
## [1] NaN
ToDo 10: Classes
data1 = c(strptime(c("20141205","20150612"),format="%Y%m%d"))
data2 = c(100,90)
plot(data1,data2, xlab = "Date",ylab="Count",main = "The number of presents I expected.")