How to select data frame columns based on their class in R?

R ProgrammingServer Side ProgrammingProgramming

To select data frame columns based on their class in R, we can follow the below steps −

  • First of all, create a data frame or consider an inbuilt data set.

  • Then, use select_if function of dplyr package with class function.

Example 1

str(CO2)

Output

On executing, the above script generates the below 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.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)"

Select columns in CO2 based on class

Using select_if function from dplyr package to select factor columns in CO2 data frame −

library(dplyr)
CO2 %>% select_if(is.factor)

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   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

Example 2

Consider the PlantGrowth data frame in base R and check it’s structure with str function −

str(PlantGrowth)

Output

On executing, the above script generates the below 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 ...

Select columns in PlantGrowth based on class

Using select_if function from dplyr package to select numeric columns in PlantGrowth data frame −

library(dplyr)
PlantGrowth %>% select_if(is.numeric)

Output

   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
raja
Published on 09-Nov-2021 07:10:06
Advertisements