数据框
#rename
patientID = c(1,2,3,4)
age = c(25,34,45,65)
diabetes = c("Type1","Type2","Type3","Type4")
patientdata = data.frame(patientID,age,diabetes)
cnames = paste("x",1:4,sep = "")
rownames(patientdata) = cnames
colnames(patientdata) = c("y1","y2","y3")
#size
ncol(patientdata)
nrow(patientdata)
nrow(na.omit(patientdata))
因子
library(datasets)
mpgData = mtcars
mpgData$am <- factor(mpgData$am, labels = c("Automatic", "Manual"))
排序
a = c(1,2,3,4,5,3,5,4,2,3,5,6)
sort(a)
rev(sort(a))
索引
a = c(1,2,3,4,5,3,5,4,2,3,5,6)
which.max(a)
a[which(a==5)]
a[which(a>5)] = 7
boo = a==5
index = which(boo)
划分数据集
index <- sample(nrow(data),0.7*nrow(data))
trainData <- data[index,]
testData <- data[-index,]
自定义函数
myfunction = function(x,a,b){
return(x+a*b)
}
myfunction(1,2,3)
curve(myfunction(x,3,2),xlim=c(1,20))
计时
ptm <- proc.time()
for (i in 1:10000)
x <- rnorm(1000)
proc.time() - ptm