当前位置:
文档之家› R语言学习系列06 修改变量名 数据排序 随机抽样
R语言学习系列06 修改变量名 数据排序 随机抽样
三、简单随机抽样
用少量数据测试数据集时,常用随机抽样方法从整体中选出部分
样本数据。
简单随机抽样,是指从总体 N 个样本中任意抽取 n 个样本,每 个样本被抽中的概率相等;分为重复抽样(有放回)、不重复抽样(不
放回)。
使用 sampling 包实现。 1. 有放回简单随机抽样 函数 srswr(),基本格式为:
rownames(x)[i]<- "newname"
> colnames(score)[5]="Chinese"
> score
student gender math Eng Chinese
1
A
M 90 88
66
2
B
M 70 78
59
3
C
F 80 69
NA
4
D
F 60 98
88
> rownames(score)=letters[1:4]
srswr(n, N)
表示从总体 N 中有放回地随机抽取 n 个样本,返回一个长度为 N 的 向量,每个分量分别表示各元素被抽取到的次数。
> library(sampling) > LETTERS [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" [12] "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" [23] "W" "X" "Y" "Z" > s<-srswr(10,26) >s
若 v1 值相同,则按 v2 升序排序;要将升序改为降序,在变量前添加
负号,或用 decreasing = TRUE 即可。
> order(score$math)
[1] 4 2 3 1
> score[order(score$math),]
student gender math Eng chinese
3.函数 order()
对数据进行排序,返回值是对应“排名”的元素所在向量中的位
置,即最小值、次小值、...、最大值所在的位置。基本格式为:
order(x,decreasing=FALSE, st= FALSE,...)
不同于前两个函数,order()还可以对数据框进行排序:
data_frame[order(data_frame$v1, data_frame$v2, …),]
其中,x 为排序对象(数值型或字符型);decreasing 默认为 FALSE 即升序,TURE 为降序;st 默认为 FALSE(NA 值将被删除), 若为 TRUE,则将向量中的 NA 值放到序列末尾。
> sort(score$math) [1] 60 70 80 90 > sort(score$math,decreasing = TRUE) [1] 90 80 70 60 > sort(score$Chinese,st = TRUE) [1] 59 66 88 NA
4
D
F 60 98
88
2
B
M 70 78
59
3
C
F 80 69
NA
1
A
M 90 88
66
> score[order(-score$math),]
student gender math Eng chinese
1
A
M 90 88
66
3
C
F 80 69
NA
2
B
M 70 78
59
4
D
F ) 求逆序,将序列进行反转,即 1,2,3 变成 3,2,1
[1] A B C D
Levels: A B C D
$gender [1] M M F F Levels: F M
$math [1] 90 70 80 60
$Eng [1] 88 78 69 98
$chinese [1] 66 59 NA 88
注意:原数据集中的变量名并未被修改。
3. 用函数 names() 和 rename()一样可用来修改数据框和列表的变量名,不能修改 矩阵的变量名;区别在于:names()会在原数据集中修改变量名。 其基本格式为:
[1] 2 0 1 1 0 0 0 0 1 0 0 2 0 0 0 3 0 0 0 0 0 0 0 [24] 0 0 0 > ind<-(1:26)[s!=0] #被抽到的样本编号 > ind [1] 1 3 4 9 12 16 > n<-s[s!=0] #被抽到的样本的被抽到的次数 >n [1] 2 1 1 1 2 3 > ind<-rep(ind,times=n) #按次数重复被抽到的样本编号 > ind [1] 1 1 3 4 9 12 12 16 16 16 > sample<-LETTERS[ind] #被抽到的字母 > sample [1] "A" "A" "C" "D" "I" "L" "L" "P" "P" "P"
3. 函数 simple()
实现有放回和不放回的简单随机抽样,基本格式为:
sample(x, size, replace = FALSE)
其中,x 为数据集;size 为抽取样本数;replace 指定是否放回,默认
为 FALSE(不放回),TURE 为有放回。
也可对数据进行随机分组:
sample(num, size, replace = TRUE, prob = NULL)
> x<-c(3,4,2,5,5,3,8,9) > rank(x) [1] 2.5 4.0 1.0 5.5 5.5 2.5 7.0 8.0 > rank(x,ties.method = "first") [1] 2 4 1 5 6 3 7 8 > rank(x,ties.method = "random") [1] 3 4 1 6 5 2 7 8 > rank(x,ties.method = "max") [1] 3 4 1 6 6 3 7 8
2.函数 rank()
返回值是该向量中对应元素的秩(排名),基本格式为:
rank(x, st= FALSE,ties.method=...)
其中,ties.method 指定对数据集中的重复数据的秩的处理方式: “average”——取平均值秩(默认) “first”——位于前面的数据的秩取小,依次递增 “random”——随机定义重复秩 “max”——取最大重复秩 “min”——取最小重复秩
2. 不放回简单随机抽样
函数 srswor(),格式和返回值同 srswr(),注意返回值向量中
只有 0 和 1.
> s<-srswor(10,26) >s [1] 1 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 1 0 0 1 [24] 1 0 1 > ind<-(1:26)[s!=0] > ind [1] 1 6 8 11 18 19 20 23 24 26 > sample<-LETTERS[ind] > sample [1] "A" "F" "H" "K" "R" "S" "T" "W" "X" "Z"
> fix(score) > score.list<-as.list(score) #将 score 转化为列表 > fix(score.list)
(1)若数据集为矩阵或数据框 将打开“数据编辑器”,单击要修改的变量名,在弹出的“变量 编辑器”修改即可:
(2)若数据集为列表 将交互式编辑器为一个记事本,只需修改“.Names”之后对应的 变量名即可:
> rename(score,c(pl="chinese"))
student gender math Eng chinese
1
A
M 90 88
66
2
B
M 70 78
59
3
C
F 80 69
NA
4
D
F 60 98
88
> rename(score.list,c(pl="chinese"))
$student
> score
student gender math Eng Chinese
a
A
M 90 88
66
b
B
M 70 78
59
c
C
F 80 69
NA
d
D
F 60 98
88
二、数据排序 1.函数 sort(),基本格式:
sort(x,decreasing=FALSE, st= FALSE,...)
主要参考文献: