【发布时间】:2020-06-29 05:54:53
【问题描述】:
我正在尝试获取在 data.table 的相应列中找到的峰值和谷值事件之间的 DT$pna 列中的最大值(即 DT$peak、DT$through)。 DT$peaks 和DT$troughs 有字符串“peak”和“trough”来标记后续事件的开始和结束。这个 for 循环使用非常少的样本,但是因为 data.table 有数百万行它需要永远。有没有更好的解决方案(可能使用数据表)在这种情况下更有效地获取最大值?
for (i in 1:nrow(DT)) {
if(is.na(DT$peak[i])) {
next
}
if(DT$peak[i] == "peak") {
e <- i + 15000
for (j in i:e) {
if(is.na(DT$trough[j])) {
next
}
if(DT$trough[j] == "trough") {
x <- (DT$pna[i:j])
}
}
}
DT[i, max_insp := max(x)]
}
【问题讨论】:
标签: r data.table processing-efficiency