mrestko
mrestko

Reputation: 133

Select list of columns from a data frame using dplyr and select_()

I'm trying to use the following function to extract some columns from a data frame:

library('dplyr')
desired_columns = c(
  'a',
  'b',
  'c')
extract_columns <- function(data) {
  extracted_data <- data %>%
    select_(desired_columns)
  return(extracted_data)
}

But when I try it, I don't get what I expect:

> df <- data.frame(a=1:5, b=1:5, c=1:5, d=1:5)
> df
  a b c d
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
> extract_columns(df)
  a
1 1
2 2
3 3
4 4
5 5

I seem to be only getting the first column and I can't figure out what I'm doing wrong. How can I get all the requested columns?

Upvotes: 4

Views: 17468

Answers (3)

Jake
Jake

Reputation: 741

A tibble is the tidyverse/dplyr version of a dataframe. Since select() is a dplyr function, you can convert your dataframe to a tibble and use select directly with a list of variables to return another tibble.

df = data.frame(a=1:5, b=1:5, c=1:5, d=1:5)
desired_columns = c( 'a', 'b', 'c')
df %>% as_tibble() %>% select(desired_columns)

Upvotes: 4

Ning
Ning

Reputation: 534

In this case, you have to use .dots parameter to pass the vector (or list):

select_(.dots = desired_columns)

It seems that it has something to do with the laziness.

Upvotes: 3

aosmith
aosmith

Reputation: 36114

You are just missing the .dots argument in select_:

extract_columns <- function(data) {
    extracted_data <- data %>%
        select_(.dots = desired_columns)
    return(extracted_data)
}

extract_columns(df)
  a b c
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5

Upvotes: 9

Related Questions