4.2 C
London
Friday, December 20, 2024
HomeTidyverse in Rdplyr in RHow to Select Only Numeric Columns in R Using dplyr

How to Select Only Numeric Columns in R Using dplyr

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories