如何使用sample_n从R数据帧中随机采样行?

要使用sample_n从R数据帧中随机采样行,我们可以直接在dplyr包的sample_n函数中传递样本大小。例如,如果我们有一个称为df的数据帧,则可以使用以下命令在df中创建5行的随机样本-

df%>%sample_n(5)

例1

考虑以下数据帧-

x1<-rnorm(20,21,3.24)
x2<-rnorm(20,5,2.1)
df1<-data.frame(x1,x2)
df1
输出结果
     x1         x2
1  21.17214  4.256648
2  24.41776  4.835844
3  22.57051  5.943711
4  18.80514  3.365967
5  21.49105  1.583063
6  20.04658  8.334448
7  16.03291  5.094798
8  21.71183  3.497276
9  18.27658  6.657804
10 15.75965  3.092517
11 24.15690  3.618473
12 25.91623  4.283104
13 24.65660  3.759834
14 18.80581  6.551064
15 25.15142  2.812932
16 20.48691  6.844323
17 25.56193  5.479819
18 22.22655  4.492731
19 20.56847  7.983841
20 23.24853  5.266284

加载dplyr软件包并在df1中随机采样10行-

library(dplyr)
df1%>%sample_n(10)
     x1         x2
1  18.80581  6.551064
2  25.91623  4.283104
3  16.03291  5.094798
4  15.75965  3.092517
5  18.27658  6.657804
6  18.80514  3.365967
7  22.57051  5.943711
8  20.04658  8.334448
9  24.41776  4.835844
10 24.15690  3.618473

例2

y1<-sample(0:1,20,replace=TRUE)
y2<-sample(LETTERS[1:5],20,replace=TRUE)
df2<-data.frame(y1,y2)
df2
输出结果
   y1 y2
1  0  E
2  0  B
3  1  E
4  0  E
5  0  D
6  1  A
7  0  D
8  1  A
9  0  E
10 0  C
11 1  E
12 1  C
13 0  A
14 0  A
15 1  D
16 0  C
17 1  D
18 0  E
19 0  D
20 0  E

在df2中随机采样10行-

df2%>%sample_n(10)

   y1 y2
1  0  E
2  0  C
3  1  E
4  1  A
5  0  A
6  1  E
7  0  D
8  0  D
9  0  D
10 0  B