繁体   English   中英

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

[英]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

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

谢谢帮忙!!

do.call(rbind, lapply(矩阵, function(x) x[which.max(x[,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], ]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM