如何在 R 中根据数据框列的类选择列?
要在 R 中根据数据框列的类选择列,我们可以按照以下步骤操作:
首先,创建一个数据框或考虑一个内置数据集。
然后,使用 dplyr 包中的 select_if 函数以及 class 函数。
示例 1
str(CO2)
输出
执行上述脚本后,将生成以下输出(由于随机化,此输出将在您的系统上有所不同):
Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':84 obs. of 5 variables: $ Plant : Ord.factor w/ 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)
输出
执行上述脚本后,将生成以下输出(由于随机化,此输出将在您的系统上有所不同):
Plant Type Treatment 1 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
考虑 base R 中的 PlantGrowth 数据框,并使用 str 函数检查其结构:
str(PlantGrowth)
输出
执行上述脚本后,将生成以下输出(由于随机化,此输出将在您的系统上有所不同):
$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)
输出
weight 1 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
广告