How to find the number of numerical columns in an R data frame?


We know that a data frame can contain any type of columns such as numerical, character, logical, factor, etc. And if a data frame contains multiple type of columns then we might want to find the number of columns for each type or of one type say numerical. For this purpose, we can use select_if function of dplyr package along with the length function as shown in the below examples.

Example1

Consider the below data frame −

Live Demo

> x1<-letters[1:20]
> x2<-rnorm(20)
> x3<-rnorm(20)
> x4<-rpois(20,5)
> df1<-data.frame(x1,x2,x3,x4)
> df1

Output

   x1      x2          x3    x4
1  a  -0.18404831  0.1082741 2
2  b  -0.28597330  0.2584625 0
3  c   1.29158108 -0.5444644 6
4  d  -0.80355312 -0.2261304 3
5  e  -0.86895219 -0.9499907 4
6  f  -0.69489165  0.4523057 3
7  g   0.70987445 -0.1152756 6
8  h   1.46023245 -1.5871850 8
9  i  -0.14756283  0.4843958 4
10 j  -1.46142329  0.7888207 12
11 k  -0.20521299 -0.6228141 9
12 l   0.76613077 -1.3652169 9
13 m   0.07624931  0.3870339 5
14 n   1.22399304  0.4028503 3
15 o  -0.09727281 -0.5263696 8
16 p  -1.97470094 -0.1248541 4
17 q   0.11621840 -1.9126845 4
18 r  -1.13008040 -1.5671634 1
19 s   0.73929690 -0.2571851 3
20 t  -0.13389093  0.4876529 4

Loading dplyr package and finding the number of numerical columns in df1 −

> library("dplyr")
> length(select_if(df1,is.numeric))
[1] 3

Example2

Live Demo

> y1<-LETTERS[1:20]
> y2<-rpois(20,2)
> y3<-rpois(20,2)
> y4<-rpois(20,10)
> y5<-sample(c("Hot","Cold"),20,replace=TRUE)
> df2<-data.frame(y1,y2,y3,y4,y5)
> df2

Output

   y1 y2 y3 y4   y5
1  A  1  1  10  Cold
2  B  2  0  24  Hot
3  C  3  0  13  Cold
4  D  1  3  14  Cold
5  E  4  4  16  Hot
6  F  3  3  9   Cold
7  G  2  2  12  Hot
8  H  3  1  6   Hot
9  I  4  0  11  Hot
10 J  2  1  14  Hot
11 K  2  0  14  Hot
12 L  1  1  8   Hot
13 M  3  2  10  Hot
14 N  0  5  9   Cold
15 O  1  2  14  Hot
16 P  1  0  13  Cold
17 Q  7  2  13  Hot
18 R  1  2  4   Cold
19 S  1  1  7   Cold
20 T  3  3  10  Hot

Finding the number of numerical columns in df −

> length(select_if(df2,is.numeric))
[1] 3

Example3

Live Demo

> z1<-sample(c("Male","Female"),20,replace=TRUE)
> z2<-rpois(20,25)
> z3<-rnorm(20,2,0.3)
> df3<-data.frame(z1,z2,z3)
> df3

Output

     z1   z2    z3
1  Female 20 2.037433
2  Female 36 2.252606
3  Female 28 2.126866
4    Male 30 1.966581
5    Male 20 1.871318
6  Female 26 2.250764
7    Male 29 1.882002
8  Female 21 1.796225
9    Male 21 2.404416
10   Male 25 1.571489
11 Female 18 2.419949
12 Male   24 1.974340
13 Male   34 1.877119
14 Male   30 2.390536
15 Female 21 1.688357
16 Female 25 1.664844
17 Male   22 2.060667
18 Male   26 1.391200
19 Female 23 1.757949
20 Female 24 1.727739

Finding the number of numerical columns in df3 −

> length(select_if(df3,is.numeric))
[1] 2

Updated on: 05-Mar-2021

782 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements