简体   繁体   中英

trying to use select in dplyr in a more compact way

I'm trying to use dplyr and the %>% symbol with multiple selection. What works:

   select(data, b1:b10) / select(data, v1:v10)

What I would like to do:

data %>% select(b1:b10)/select(v1:v10)   

What about

#sample data
data<-data.frame(matrix(runif(20*45), ncol=20, 
    dimnames=list(NULL, paste0(rep(c("b","v"), each=10),1:10))) )

#orig
A<-select(data, b1:b10) / select(data, v1:v10)

#proposed
B<-data %>% function(X) {select(X, b1:b10)/select(X, v1:v10)}

#verify they are the same
all(A==B)

I also have a helper function called withX that i use a lot in situations like this

C <- data %>% withX( select(X, b1:b10) / select(X, v1:v10) )
all(C==A)
# [1] TRUE

Or maybe in this case we might like another helper function for working the the chain and creating a list form different subsets. Consider the function each

each<-function(.data, ...) {
    dots <- substitute(list(...))[-1]
    force(.data)
    e<-environment()
    parent.env(e)<-parent.frame()
    lapply(dots, function(x) {
        eval(bquote(.data %>% .(x)), e)
    })
}

This function will perform the dplyr chaining for each parameter you pass in. So you can do

D <- data %>% each(select(b1:b10), select(v1:v10)) %>% Reduce(`/`, .)
all(A==D)
# [1] TRUE

Now I realize all these alternative methods aren't "compact" in the sense of fewer characters. But you do only have to specify the data.frame name once. So there's that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM