R语言 迭代过滤器列表

fdx2calv  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(106)

我想在一个过滤器的组合列表中添加一个过滤器组合,这些过滤器组合应用于一个类似于以下的嵌套框架的多个列:

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)
)

字符串
举个例子:
我想计算以下组合的var 1的值:

filter1: ID1=='Asset1' and ID2=='Asset1'


filter2: ID1=='Asset1' and ID2=='Asset2'


对于一个组合,我显然可以这样做,用一个dspur过滤器:

library(dplyr)
df %>% filter(ID1=='Asset1', ID2=='Asset1')


但是我想用一个for循环来迭代所有的过滤器:

filter_list <- c(filter1, filter2)

for (filter_i in filter_list){
    df %>% filter(filter_i)
}

iaqfqrcu

iaqfqrcu1#

使用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

字符串

hk8txs48

hk8txs482#

您可以从1:2列中的unique资产创建过滤器列表。接下来,使用expand.grid使用所有2-排列生成filter_list

> assets <- unique(unlist(df[1:2]))
> filter_list <- expand.grid(assets, assets) |> asplit(1)

字符串
最后,你可以用subset覆盖列表。

> lapply(filter_list, \(x) subset(x=df, ID1 == x[1] & ID2 == x[2]))
[[1]]
     ID1    ID2   var1
1 Asset1 Asset1 0.0110
5 Asset1 Asset1 0.0011

[[2]]
     ID1    ID2   var1
2 Asset2 Asset1 0.0120
6 Asset2 Asset1 0.0012

[[3]]
     ID1    ID2   var1
3 Asset1 Asset2 0.0120
7 Asset1 Asset2 0.0012

[[4]]
     ID1    ID2   var1
4 Asset2 Asset2 0.0220
8 Asset2 Asset2 0.0022


或者,如果您坚持使用for循环,

> res <- vector('list', length(filter_list))  ## initialize
> for (i in seq_along(filter_list)) {
+   res[[i]] <- subset(x=df, ID1 == filter_list[[i]][1] & ID2 == filter_list[[i]][2])
+ }
> res
[[1]]
     ID1    ID2   var1
1 Asset1 Asset1 0.0110
5 Asset1 Asset1 0.0011

[[2]]
     ID1    ID2   var1
2 Asset2 Asset1 0.0120
6 Asset2 Asset1 0.0012

[[3]]
     ID1    ID2   var1
3 Asset1 Asset2 0.0120
7 Asset1 Asset2 0.0012

[[4]]
     ID1    ID2   var1
4 Asset2 Asset2 0.0220
8 Asset2 Asset2 0.0022

  • 数据类型:*
> dput(df)
structure(list(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, 0.0011, 0.0012, 0.0012, 
    0.0022)), class = "data.frame", row.names = c(NA, -8L))

相关问题