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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With