我需要找到列中的資料達到某個值的行數。
資料:
Data = data.frame( Time = seq(1,5),
V1 = c(0.1, 0.2, 0.4, 0.7, 0.9),
v2 = c(0.1, 0.12, 0.41, 0.72, 0.91),
v3 = c(0.03, 0.13, 0.62, 0.50, 0.90))
預期結果:(第一次在 0.6)
V1 V2 V3
1 4 4 3
uj5u.com熱心網友回復:
我們可以用dplyr與summarise和first(which(condition))
library(dplyr)
Data %>% summarise(across(-Time, ~first(which(.x>0.6))))
V1 v2 v3
1 4 4 3
或使用基礎 R:
data.frame(lapply(Data[-1], \(x) first(which(x>0.6))))
uj5u.com熱心網友回復:
使用基礎 R:
使用which地發現,滿足您的條件行的索引,并min找到最低的(即,第一),這些指數。也用于lapply將其應用于多個列,如下所示:
data.frame(lapply(Data[c("V1", "v2", "v3")], function(x) min(which(x > 0.6))))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/354164.html
