How to Select Only Numeric Columns in R Using dplyr

Using the dplyr package in R, it is possible to select only numeric columns from a dataframe by using the select_if() function. This function takes a predicate as an argument, which in the case of selecting only numeric columns is is.numeric(), and returns a dataframe containing only the columns which fulfill the predicate. This is a useful tool for quickly subsetting a dataframe and selecting only the desired columns.


You can use the following function from the package to select only numeric columns from a data frame in R:

df %>% select(where(is.numeric))

The following example shows how to use this function in practice.

Example: Select Only Numeric Columns Using dplyr

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(22, 34, 30, 12, 18),
                 assists=c(7, 9, 9, 12, 14),
                 rebounds=c(5, 10, 10, 8, 8))

#view data frame
df

  team points assists rebounds
1    A     22       7        5
2    B     34       9       10
3    C     30       9       10
4    D     12      12        8
5    E     18      14        8

We can use the following syntax to select only the numeric columns from the data frame:

library(dplyr)

#select only the numeric columns from the data frame
df %>% select(where(is.numeric))

  points assists rebounds
1     22       7        5
2     34       9       10
3     30       9       10
4     12      12        8
5     18      14        8

Notice that only the three numeric columns have been selected – points, assists, and rebounds.

We can verify that these columns are numeric by using the str() function to display the data type of each variable in the data frame:

#display data type of each variable in data frame
str(df)

'data.frame':	5 obs. of  4 variables:
 $ team    : chr  "A" "B" "C" "D" ...
 $ points  : num  22 34 30 12 18
 $ assists : num  7 9 9 12 14
 $ rebounds: num  5 10 10 8 8

From the output we can see that team is a character variable while points, assists, and rebounds are all numeric.

Related:

The following tutorials explain how to perform other common tasks using dplyr:

x