【问题标题】:R: How to merge logical elements in a listR:如何合并列表中的逻辑元素
【发布时间】:2015-01-04 19:01:25
【问题描述】:

我有如下数据

set.seed(1)
glist <- lapply(1:5,function(i){sample(c(TRUE,FALSE),1)})

我想要 FALSE 值的索引位置 c(3,4)。我正在尝试将列表数组强制转换为数组

tf_array <- Reduce(merge,glist)

输出是

[1] y x
<0 rows> (or 0-length row.names)

我期望的输出如下,然后我打算使用 which 函数。

c(TRUE,TRUE,FALSE,FALSE,TRUE).

如何在逻辑值列表中找到 FALSE 条目的索引?

【问题讨论】:

    标签: r list reduce


    【解决方案1】:

    unlist(glist) 给出你期望的输出:

    [1]  TRUE  TRUE FALSE FALSE  TRUE
    

    然后,使用which()

    which(!unlist(glist))
    # [1] 3 4
    

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 2013-02-11
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 2021-11-29
      • 2016-05-05
      相关资源
      最近更新 更多