English 中文(简体)
在R的筛选清单中添加
原标题:Iterate over a list of filters in R

I would like to iterate over a a list of combinations of filters that are applied to the multiple columns of a dataframe that looks like this:

df <- data.frame(    
    ID1 = c("Asset1", "Asset2", "Asset1", "Asset2", "Asset1", "Asset2", "Asset1", "Asset2"),
    ID2 = c("Asset1", "Asset1", "Asset2", "Asset2", "Asset1", "Asset1", "Asset2", "Asset2"),
    var1= c(0.011,      0.012,   0.012,    0.022,   .0011,      0.0012,   0.0012,    0.0022)
)

An example:

我要赞扬var1对以下组合的价值:

filter1: ID1== Asset1  and ID2== Asset1 

and

filter2: ID1== Asset1  and ID2== Asset2 

仅对一种组合而言,我就能够用一种yr子过滤器作此处理:

df %>% filter(ID1== Asset1 , ID2== Asset1 )

但是,我要用一种 lo子来做到这一点,它会渗透到所有过滤器上:

filter_list<- c(filter1, filter2)

for (filter_i in filter_list){
    df %>% filter(filter_i)
}
问题回答

采用quos 和Bang-bang-bang aka !

library(dplyr, warn = FALSE)

filters <- list(
  filter1 = quos(ID1 == "Asset1", ID2 == "Asset1"),
  filter2 = quos(ID1 == "Asset1", ID2 == "Asset2")
)

lapply(filters, (x) {
  df %>%
    filter(!!!x)
})
#> $filter1
#>      ID1    ID2   var1
#> 1 Asset1 Asset1 0.0110
#> 2 Asset1 Asset1 0.0011
#> 
#> $filter2
#>      ID1    ID2   var1
#> 1 Asset1 Asset2 0.0120
#> 2 Asset1 Asset2 0.0012




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...