How to Select Only Numeric Columns in R Using dplyr


You can use the following function from the dplyr 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: How to Check Data Type in R (With Examples)

Additional Resources

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

How to Select Columns by Name Using dplyr
How to Select Columns by Index Using dplyr
How to Use select_if with Multiple Conditions in dplyr

Leave a Reply

Your email address will not be published.