Syntax error How to select data.table object columns based on their class in R?

How to select data.table object columns based on their class in R?



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

  • First of all, create a data.table object.

  • Then, use str function to check the structure of the object.

  • After that, use select_if function from dplyr package to select the columns based on their class.

Example 1

Create the data.table object

Let’s create a data.table object as shown below −

library(data.table)
x1<-sample(LETTERS[1:4],25,replace=TRUE)
x2<-rnorm(25)
x3<-rpois(25,5)
DT1<-data.table(x1,x2,x3)
DT1

Output

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

    x1    x2     x3
1:  D -1.03911797 6
2:  C -0.18664587 3
3:  D -1.36429362 3
4:  A -2.26587126 8
5:  B  0.10786571 3
6:  C  0.01271455 4
7:  D  1.33889909 8
8:  C  2.24053400 9
9:  B  1.45987567 9
10: B  0.16747607 7
11: C -0.39258915 3
12: A  0.22643666 7
13: A -0.19909780 7
14: D -1.37518544 7
15: D -1.47161101 4
16: C  0.95574993 7
17: B  0.86833240 5
18: A  0.24516224 5
19: C -1.25342994 6
20: A  1.46559041 2
21: C  0.34863015 2
22: D -0.33877737 5
23: B  0.26071352 4
24: D -0.61747246 4
25: A -0.35990471 7
   x1    x2       x3

Check the structure of data.table object

Using str function to check the structure of the data.table object DT1 −

library(data.table)
x1<-sample(LETTERS[1:4],25,replace=TRUE)
x2<-rnorm(25)
x3<-rpois(25,5)
DT1<-data.table(x1,x2,x3)
str(DT1)

Output

Classes ‘data.table’ and 'data.frame': 25 obs. of 3 variables:
$ x1: chr "D" "C" "D" "A" ...
$ x2: num -1.039 -0.187 -1.364 -2.266 0.108 ...
$ x3: int 6 3 3 8 3 4 8 9 9 7 ...
- attr(*, ".internal.selfref")=<externalptr>

Select columns based on their class

Using select_if function from dplyr package to select integer columns as shown below −

library(data.table)
x1<-sample(LETTERS[1:4],25,replace=TRUE)
x2<-rnorm(25)
x3<-rpois(25,5)
DT1<-data.table(x1,x2,x3)
str(DT1)
library(dplyr)
DT1 %>% select_if(is.integer)

Output

   x3
1:  6
2:  3
3:  3
4:  8
5:  3
6:  4
7:  8
8:  9
9:  9
10: 7
11: 3
12: 7
13: 7
14: 7
15: 4
16: 7
17: 5
18: 5
19: 6
20: 2
21: 2
22: 5
23: 4
24: 4
25: 7
    x3

Example 2

Create the data.table object

Let’s create a data.table object as shown below −

library(data.table)
y1<-sample(c(TRUE,FALSE),25,replace=TRUE)
y2<-factor(sample(c("I","II","III"),25,replace=TRUE))
DT2<-data.table(y1,y2)
DT2

Output

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

     y1   y2
1:  TRUE  III
2:  FALSE III
3:  TRUE  I
4:  FALSE II
5:  FALSE III
6:  FALSE I
7:  TRUE  II
8:  FALSE II
9:  TRUE  II
10: FALSE III
11: TRUE  I
12: TRUE  I
13: TRUE  II
14: FALSE II
15: TRUE  I
16: FALSE III
17: FALSE I
18: FALSE II
19: TRUE  II
20: FALSE II
21: TRUE  III
22: TRUE  I
23: TRUE  I
24: TRUE  II
25: FALSE III
     y1   y2

Select columns based on their class

Using select_if function from dplyr package to select logical columns as shown below −

library(data.table)
y1<-sample(c(TRUE,FALSE),25,replace=TRUE)
y2<-factor(sample(c("I","II","III"),25,replace=TRUE))
DT2<-data.table(y1,y2)
library(dplyr)
DT2 %>% select_if(is.logical)

Output

     y1
1:  TRUE
2:  FALSE
3:  TRUE
4:  FALSE
5:  FALSE
6:  FALSE
7:  TRUE
8:  FALSE
9:  TRUE
10: FALSE
11: TRUE
12: TRUE
13: TRUE
14: FALSE
15: TRUE
16: FALSE
17: FALSE
18: FALSE
19: TRUE
20: FALSE
21: TRUE
22: TRUE
23: TRUE
24: TRUE
25: FALSE
     y1
Updated on: 2021-11-12T07:12:53+05:30

478 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements