如何从 R 数据框中选择一列并将其作为数据框而不是向量返回?


通常,如果我们从 R 数据框中提取一列,则它会被提取为一个向量,但我们可能希望将其以数据框的形式提取,以便我们可以对其应用数据框的操作。因此,我们可以使用单个方括号进行提取,并使用 T(真)或(假)值以及 drop = FALSE,以便输出成为一个数据框。

考虑以下数据框:

示例

 在线演示

set.seed(999)
x1<-rnorm(20,5,1)
x2<-rnorm(20,5,2)
x3<-rnorm(20,10,2)
x4<-rnorm(20,10,1.5)
x5<-rnorm(20,10,4)
df1<-data.frame(x1,x2,x3,x4,x5)
df1

输出

       x1      x2        x3     x4      x5
1 4.718260 2.542873 9.028727 8.615033 7.428309
2 3.687440 6.286089 10.016996 11.747431 13.941194
3 5.795184 4.280474 7.435773 11.563103 5.085707
4 5.270070 5.588071 7.776842 11.655743 10.340899
5 4.722694 2.749463 10.601331 9.972138 5.183625
6 4.433976 6.284531 10.552958 8.279563 8.492609
7 3.121342 2.786525 5.898245 7.887731 15.454594
8 3.733209 3.230319 10.028380 9.576507 8.988469
9 4.032250 1.891810 11.164533 9.373345 14.025960
10 3.878991 4.746642 9.930547 11.494995 11.748597
11 6.325464 9.765328 9.766672 9.840571 16.634154
12 5.133977 6.202552 8.710036 9.895897 10.110741
13 5.938749 5.358723 13.488823 11.424552 6.085122
14 5.172538 7.161063 10.732189 9.375201 15.134917
15 5.957650 4.506376 9.866380 11.461001 5.481034
16 3.637314 0.772526 10.565225 10.093437 14.186631
17 5.068335 4.258945 11.135390 10.807633 7.688271
18 5.100658 6.045736 7.441568 6.902765 8.824823
19 5.901345 6.035611 10.870738 10.654647 9.032894
20 2.925643 2.194978 8.868998 9.759660 10.879791

提取列 x1:

df1[,c(T,F,F,F,F)]

[1] 4.718260 3.687440 5.795184 5.270070 4.722694 4.433976 3.121342 3.733209
[9] 4.032250 3.878991 6.325464 5.133977 5.938749 5.172538 5.957650 3.637314
[17] 5.068335 5.100658 5.901345 2.925643

is.vector(df1[,c(T,F,F,F,F)]) [1] TRUE

将列 x1 提取为数据框:

df1[,c(T,F,F,F,F),drop=FALSE]

x1
1 4.718260
2 3.687440
3 5.795184
4 5.270070
5 4.722694
6 4.433976
7 3.121342
8 3.733209
9 4.032250
10 3.878991
11 6.325464
12 5.133977
13 5.938749
14 5.172538
15 5.957650
16 3.637314
17 5.068335
18 5.100658
19 5.901345
20 2.925643

以相同的方式提取不同的列:

df1[,c(F,T,F,F,F),drop=FALSE]

  x2
1 2.542873
2 6.286089
3 4.280474
4 5.588071
5 2.749463
6 6.284531
7 2.786525
8 3.230319
9 1.891810
10 4.746642
11 9.765328
12 6.202552
13 5.358723
14 7.161063
15 4.506376
16 0.772526
17 4.258945
18 6.045736
19 6.035611
20 2.194978

df1[,c(F,F,T,F,F),drop=FALSE]

x3
1 9.028727
2 10.016996
3 7.435773
4 7.776842
5 10.601331
6 10.552958
7 5.898245
8 10.028380
9 11.164533
10 9.930547
11 9.766672
12 8.710036
13 13.488823
14 10.732189
15 9.866380
16 10.565225
17 11.135390
18 7.441568
19 10.870738
20 8.868998

df1[,c(F,F,F,F,T),drop=FALSE]

x5
1 7.428309
2 13.941194
3 5.085707
4 10.340899
5 5.183625
6 8.492609
7 15.454594
8 8.988469
9 14.025960
10 11.748597
11 16.634154
12 10.110741
13 6.085122
14 15.134917
15 5.481034
16 14.186631
17 7.688271
18 8.824823
19 9.032894
20 10.879791

更新于: 2020年10月10日

296 次查看

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告