开发者学堂课程【大数据之 R 语言速成与实战:R 基本数据管理--数据集合并、子集的提取以及随机抽样函数】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/363/detail/4335
R 基本数据管理--数据集合并、子集的提取以及随机抽样函数
内容简介:
一、数据集合并
二、数据取子集
三、随机抽样函数
一、数据集合并
>x<- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
>x
> y<-x
> y
> z <- cbind(x,y)
> z
>?merge
>x <- data.frame(k1 = c
(NA,NA,3,4,5),k2 =c(1,NA,NA,4,5),
data = 1:5
)
>x
> y
> z <-merge(x,y
,by=”k1”)
> z
> y<-x
>X
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> y
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> z <-rbind(x,y)
> z
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[,4] 1 4 7
[,5] 2 5 8
[,6] 3 6 9
>X[,2:3]
[,1] [,2]
[1,] 4 7
[2,] 5 8
[3,] 6 9
>manager <-c(1,2,3,4,5)
>date<-c("10/24/08",'10/28/08","10/1/08","10/12/08","5/1/09")
>country <-c("US","US","UK","UK","UK")
>gender <- c("M","F","F","M","F")
>age <- c(32,45,25,39,99)
>q1<-c(5,3.3.3.2)
>q2<-c(4.5.5.,3.2)
>q3<-c
(5,2,5,4,1)
>q4<-c(5,5,5,NA,2)
>q5<-c(5,5,2,NA,1)
>survey<-data.frame(manager,date,country,gender,age,q1,q2,q3,q4, q5,stringsAsFactors=FALSE)
>
>survey
二、数据取子集
>survey
>q<- survey[,6:10]
>q
>x<- survey[,-2]
>x
三、随机抽样函数
>survey
> mysample<- survey[sample(5,3,replace=FALSE),]
> mysample
>newdata <- subset(survey,age>=35age<24,
select=c(ql,q2,q3,q4))
> newdata
> mysample<- survey[sample(5,3,replace=FALSE),]
> mysample
> mysample<- survey[sample(5,3,replace=FALSE),]
> mysample