【问题标题】:How to extract first observation satisfying a condition within a list of matrix?如何在矩阵列表中提取满足条件的第一个观察?
【发布时间】:2020-04-25 15:30:01
【问题描述】:

我有一个来自模拟的 100 个矩阵的列表。每个矩阵是 100x3。我需要生成一个新矩阵,其中包含列表中每个矩阵中满足 matrix[,2]>9 的第一个观察值。

我使用以下代码做了类似的事情(生成一个包含列表中矩阵的第一个观察值的矩阵)。

oferta_1=do.call(rbind,lapply(matrices, head, 1)) #where matrices is a list of 100 matrix 100x3

除了条件“满足矩阵 [,2]>9 的第一个观察”,我该如何做同样的事情?

感谢您的帮助!!

【问题讨论】:

    标签: r list matrix simulation


    【解决方案1】:

    do.call(rbind, lapply(矩阵, function(x) x[which.max(x[,2] > 9), ]))

    【讨论】:

      【解决方案2】:

      你可以试试:

      oferta_1 <- do.call(rbind,lapply(matrices, function(x) x[which.max(x[,2] > 9), ]))
      

      或者可能更安全:

      oferta_1 <- do.call(rbind, lapply(matrices, function(x) x[which(x[,2] > 9)[1], ]))
      

      【讨论】:

        猜你喜欢
        • 2020-02-11
        • 2014-06-03
        • 1970-01-01
        • 2016-01-29
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 2019-12-22
        • 1970-01-01
        相关资源
        最近更新 更多