如何提取满足矩阵列表中条件的第一个观察值?

How to extract first observation satisfying a condition within a list of matrix?

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

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

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

我如何做同样的事情,但条件是 "first observation that satisfy matrix[,2]>9"?

感谢帮助!!

你可以试试:

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

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