【问题标题】:How can I Filter a list of S4 objects conditional on TRUE/FALSE statement in another list如何在另一个列表中过滤以 TRUE/FALSE 语句为条件的 S4 对象列表
【发布时间】:2022-01-23 08:47:59
【问题描述】:

我有一个来自 rpact 包的 S4 对象列表,我想根据另一个列表中的 TRUE/FALSE 语句有条件地过滤它。目标是返回根据另一个列表中的 TRUE/FALSE 语句过滤的 S4 对象列表(即,仅返回具有匹配 TRUE 语句的 S4 对象)。

install.packages("rpact")
library(rpact)

# list with S4 objects
list1 <- list(getDesignGroupSequential(sided = 1, alpha = 0.025, informationRates = c(0.33, 0.67, 1), typeOfDesign = "OF"),
              getDesignGroupSequential(sided = 1, alpha = 0.025, informationRates = c(0.33, 0.67, 1), typeOfDesign = "OF"),
              getDesignGroupSequential(sided = 1, alpha = 0.025, informationRates = c(0.33, 0.67, 1), typeOfDesign = "OF"))

list2 <- list(TRUE, FALSE, TRUE)

我尝试使用mappy,就像使用包含数据集的列表一样。然而,这产生了以下错误:x[y == TRUE] 中的错误:'S4' 类型的对象不可子集

# mapply function
list3 <- mapply(function(x, y) x[y == TRUE], list1, list2)

你知道如何解决这个问题吗?非常感谢任何帮助。

【问题讨论】:

  • list1[unlist(list2)] 应该可以正常工作。您的 mapply 正在尝试过滤 list1 内部的对象,而不是 list1 本身。但如果我理解正确,您想要list1 的一个子集,而list1 包含什么并不重要。
  • @GregorThomas 这实际上是我的意思。从来不知道会这么简单。感谢您的帮助,非常感谢。

标签: r list conditional-statements filtering


【解决方案1】:
list3 <- list1[list2 == TRUE]

【讨论】:

  • 这个答案应该更具描述性,解释代码的作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多