r - Select list of columns from a data frame using dplyr and select_() -
i'm trying use following function extract columns 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 try it, don't expect:
> df <- data.frame(a=1:5, b=1:5, c=1:5, d=1:5) > df 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) 1 1 2 2 3 3 4 4 5 5
i seem getting first column , can't figure out i'm doing wrong. how can requested columns?
you missing .dots
argument in select_
:
extract_columns <- function(data) { extracted_data <- data %>% select_(.dots = desired_columns) return(extracted_data) } extract_columns(df) b c 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5
Comments
Post a Comment