1.画网格
abline(h=seq(1,10,.5),col="blue")
abline(v=seq(1, 10, .5), lty=2, col="blue") ###参考线(垂直线)
2.2Data structure
a <- c(1, 2, 5, 3, 6, -2, 4) ##vector
a[c(2,3, 4)]
rnames <- c("R1", "R2") # matrix with rowname
cnames <- c("C1", "C2")
mymatrix <- matrix(c(1,2,3,1,9,6), nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames))
dim1 <- c("A1", "A2")
dim2 <- c("B1", "B2", "B3")
dim3 <- c("C1", "C2", "C3", "C4")
z <- array(1:24, c(2, 3, 4), dimnames=list(dim1, dim2, dim3)) ## array 数组
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID,age, diabetes, status) ## data.frame 数据框
table(patientdata$diabetes, patientdata$status)
patientdata <- data.frame(patientID, age, diabetes, status,row.names=patientID)
status <- c("Poor", "Improved", "Excellent", "Poor")
status <- factor(status, ordered=TRUE) ## facotr 因子(有序无序)
status <- factor(status, order=TRUE,levels=c("Poor", "Improved", "Excellent"))
g <- "My First List"
h <- c(25, 26, 18, 39)
j <- matrix(1:10, nrow=5)
k <- c("one", "two", "three")
mylist <- list(title=g, ages=h, j, k) ## list 列表,组合四种components: string, vector,matrix,character vector
##2.3 数据输入
## 1.直接在R上输入; for small datasets
mydata <- data.frame(age=numeric(0), gender=character(0), weight=numeric(0))
mydata <- edit(mydata)
## 2.data from a delimited text file
mydataframe <- read.table(file, header=TRUE or FALSE, sep="delimiter", row.names="name")
grades <- read.table("studentgrades.csv", header=TRUE, sep=",", row.names="STUDENTID") #例子
##3. Importing data from Excel
data1<-read.csv("studentgrades.csv",sep=",",header=TRUE) ##直接导入csv
library(RODBC) ##use the RODBC package to access Excel files
channel <- odbcConnectExcel("studentgrades.xls")
mydataframe <- sqlFetch(channel, "mysheet")
odbcClose(channel)
## 第三章 graphs
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type="b")
par(pin=c(4,3), mai=c(1,.5, 1, .2)) ##图形尺寸,如生成4英寸宽、3高、上下边界为1、左0.5、右0.2的图
plot(dose, d