Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting with dplyr by parameters in column names

Tags:

r

dplyr

I have data table with a lots of columns looking like this:

sd1_scale1 sd1_scale2 sd1_scale3 ... sd2_scale1 sd2_scale2 ... so on

I manipulate this data with dplyr and use select in this way:

  select(code_group, sd1_scale1:sd1_scale13)

I want to write function which takes number (sd number) and selects columns by this, something looking like this:

 makeData <- function(sdNumber) {

       return select(code_group, sd{sdNumber}_scale1:sd{sdNumber}_scale13)

    }

Is it possible to do with dplyr? I've failed to pass into select indexes of columns so I have no idea how to do it. Thanks in advance!

like image 989
Dmitry Malugin Avatar asked Jan 06 '15 19:01

Dmitry Malugin


Video Answer


1 Answers

You can use select_ as Gregor suggested, but you don't have to.

library(dplyr)

x <- read.csv(text = "sd1_scale1,sd1_scale2,sd1_scale3,sd2_scale1,sd2_scale2,sd2_scale3
1,2,3,4", header = TRUE)


makeData1 <- function(x, sdNumber) {
  # Using `one_of` as explained in ?select
  select(x, one_of(paste0("sd", sdNumber, "_scale", 1:2)))
}

makeData2 <- function(x, sdNumber) {
  # Same effect using nonstandard evaluation, see vignette("nse")
  select_(x, .dots = paste0("sd", sdNumber, "_scale", 1:2))
}

x %>% makeData1(2)    
x %>% makeData2(2) # same result

I got this from this gist

like image 167
nacnudus Avatar answered Sep 20 '22 00:09

nacnudus