如何根据 R 中的类选择数据框列?
要根据 R 中的类选择数据框列,我们可以按照以下步骤操作 -
首先,创建一个数据框或考虑一个内置的数据集。
然后,使用带有类函数的 dplyr 包的 select_if 函数。
示例 1
str(CO2)输出结果
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':84 obs. of 5 variables:$ Plant :Ord.factorw/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
$ Type : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
$ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
$ conc : num 95 175 250 350 500 675 1000 95 175 250 ...
$ uptake : num 16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...
- attr(*, "formula")=Class 'formula' language uptake ~ conc | Plant
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "outer")=Class 'formula' language ~Treatment * Type
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "labels")=List of 2
..$ x: chr "Ambient carbon dioxide concentration"
..$ y: chr "CO2 uptake rate"
- attr(*, "units")=List of 2
..$ x: chr "(uL/L)"
..$ y: chr "(umol/m^2 s)"
根据类别选择 CO2 中的列
使用 dplyr 包中的 select_if 函数选择 CO2 数据框中的因子列 -
library(dplyr)输出结果CO2 %>% select_if(is.factor)
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
Plant Type Treatment1 Qn1 Quebec nonchilled
2 Qn1 Quebec nonchilled
3 Qn1 Quebec nonchilled
4 Qn1 Quebec nonchilled
5 Qn1 Quebec nonchilled
6 Qn1 Quebec nonchilled
7 Qn1 Quebec nonchilled
8 Qn2 Quebec nonchilled
9 Qn2 Quebec nonchilled
10 Qn2 Quebec nonchilled
11 Qn2 Quebec nonchilled
12 Qn2 Quebec nonchilled
13 Qn2 Quebec nonchilled
14 Qn2 Quebec nonchilled
15 Qn3 Quebec nonchilled
16 Qn3 Quebec nonchilled
17 Qn3 Quebec nonchilled
18 Qn3 Quebec nonchilled
19 Qn3 Quebec nonchilled
20 Qn3 Quebec nonchilled
21 Qn3 Quebec nonchilled
22 Qc1 Quebec chilled
23 Qc1 Quebec chilled
24 Qc1 Quebec chilled
25 Qc1 Quebec chilled
26 Qc1 Quebec chilled
27 Qc1 Quebec chilled
28 Qc1 Quebec chilled
29 Qc2 Quebec chilled
30 Qc2 Quebec chilled
31 Qc2 Quebec chilled
32 Qc2 Quebec chilled
33 Qc2 Quebec chilled
34 Qc2 Quebec chilled
35 Qc2 Quebec chilled
36 Qc3 Quebec chilled
37 Qc3 Quebec chilled
38 Qc3 Quebec chilled
39 Qc3 Quebec chilled
40 Qc3 Quebec chilled
41 Qc3 Quebec chilled
42 Qc3 Quebec chilled
43 Mn1 Mississippi nonchilled
44 Mn1 Mississippi nonchilled
45 Mn1 Mississippi nonchilled
46 Mn1 Mississippi nonchilled
47 Mn1 Mississippi nonchilled
48 Mn1 Mississippi nonchilled
49 Mn1 Mississippi nonchilled
50 Mn2 Mississippi nonchilled
51 Mn2 Mississippi nonchilled
52 Mn2 Mississippi nonchilled
53 Mn2 Mississippi nonchilled
54 Mn2 Mississippi nonchilled
55 Mn2 Mississippi nonchilled
56 Mn2 Mississippi nonchilled
57 Mn3 Mississippi nonchilled
58 Mn3 Mississippi nonchilled
59 Mn3 Mississippi nonchilled
60 Mn3 Mississippi nonchilled
61 Mn3 Mississippi nonchilled
62 Mn3 Mississippi nonchilled
63 Mn3 Mississippi nonchilled
64 Mc1 Mississippi chilled
65 Mc1 Mississippi chilled
66 Mc1 Mississippi chilled
67 Mc1 Mississippi chilled
68 Mc1 Mississippi chilled
69 Mc1 Mississippi chilled
70 Mc1 Mississippi chilled
71 Mc2 Mississippi chilled
72 Mc2 Mississippi chilled
73 Mc2 Mississippi chilled
74 Mc2 Mississippi chilled
75 Mc2 Mississippi chilled
76 Mc2 Mississippi chilled
77 Mc2 Mississippi chilled
78 Mc3 Mississippi chilled
79 Mc3 Mississippi chilled
80 Mc3 Mississippi chilled
81 Mc3 Mississippi chilled
82 Mc3 Mississippi chilled
83 Mc3 Mississippi chilled
84 Mc3 Mississippi chilled
示例 2
考虑基础 R 中的 PlantGrowth 数据框并使用 str 函数检查它的结构 -
str(PlantGrowth)输出结果
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
$Rscript main.r'data.frame':30 obs. of 2 variables:
$ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
$ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
根据类选择 PlantGrowth 中的列
使用 dplyr 包中的 select_if 函数选择 PlantGrowth 数据框中的数字列 -
library(dplyr)输出结果PlantGrowth %>% select_if(is.numeric)
weight1 4.17
2 5.58
3 5.18
4 6.11
5 4.50
6 4.61
7 5.17
8 4.53
9 5.33
10 5.14
11 4.81
12 4.17
13 4.41
14 3.59
15 5.87
16 3.83
17 6.03
18 4.89
19 4.32
20 4.69
21 6.31
22 5.12
23 5.54
24 5.50
25 5.37
26 5.29
27 4.92
28 6.15
29 5.80
30 5.26
以上是 如何根据 R 中的类选择数据框列? 的全部内容, 来源链接: utcz.com/z/335437.html