Selecting columns based on type

[This article was first published on R on Abhijit Dasgupta, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The tidyverse and, in particular, dplyr, provides functions to select columns from a data frame. There are three scoped functions available: select_all, select_if and select_at. In this post, we’ll look at a particular application of select_if, i.e., capturing the names of numeric variables.

A quick search using Google finds a few solutions to this problem. As an example data set, I’ll use the diamonds data set from the ggplot2 package.

names(diamonds)[sapply(diamonds, is.numeric)]
## [1] "carat" "depth" "table" "price" "x"     "y"     "z"

or, equivalently

names(diamonds)[map_lgl(diamonds, is.numeric)]
## [1] "carat" "depth" "table" "price" "x"     "y"     "z"

However, there is an elegant (to me) pipeline based solution using select_if.

diamonds %>% select_if(is.numeric) %>% names()
## [1] "carat" "depth" "table" "price" "x"     "y"     "z"

However, the elegance is at the expense of some efficiency.

library(microbenchmark)
microbenchmark(names(diamonds)[sapply(diamonds, is.numeric)],
               names(diamonds)[map_lgl(diamonds, is.numeric)],
               diamonds %>% select_if(is.numeric) %>% names()) %>% 
  autoplot()
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

To leave a comment for the author, please follow the link and comment on their blog: R on Abhijit Dasgupta.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)