- 数据采样:
setwd("E:\\Rwork")
set.seed(1234)
index <- sample(1:nrow(iris),10, replace = T)
index
sample_set <- iris[index,]
index <- sample(nrow(iris),0.75*nrow(iris))
sample_set <- iris[index,]
- 数值离散化
data(iris)
buckets <- 10
maxseplen <- max(iris$Sepal.Length)
minseplen <- min(iris$Sepal.Length)
cutpoints <- seq(minseplen, maxseplen, by = (maxseplen - minseplen ) / buckets )
cutpoints
cutseplen <- cut(iris$Sepal.Length, breaks = cutpoints , include.lowest = TRUE)
newiris <- data.frame(contseplen = iris$Sepal.Length , discseplen = cutseplen)
newiris
- 数据合并
最常用merge()函数,但是这个函数使用时候这两种情况需要注意:
1、merge(a,b),纯粹地把两个数据集合在一起,没有沟通a、b数据集的by,这样出现的数据很多,相当于a*b条数据;
2、merge函数是匹配到a,b数据集的并,都有的才匹配出来,如果a、b数据集ID不同,要用all=T(下面有all用法的代码)。
ID<-c(1,2,3,4)
name<-c("Jim","Tony","Lisa","Tom")
score<-c(89,22,78,78)
student1<-data.frame(ID,name)
student2<-data.frame(ID,score)
total_student<-merge(student1,student2,by="ID") #或者rbind()
total_student
ID<-c(1,2,3)
name<-c("Jame","Kevin","Sunny")
student1<-data.frame(ID,name)
ID<-c(4,5,6)
name<-c("Sun","Frame","Eric")
student2<-data.frame(ID,name)
total<-cbind(student1,student2)
total
网友评论