【问题标题】:In a time series, find the longest subsequent period for which a condition is met在时间序列中,找到满足条件的最长后续期间
【发布时间】:2016-06-27 11:33:21
【问题描述】:

我需要通过dataframe进行搜索,如下: 如果等级在时间 = 1 时高于 50%,然后在时间 = 3 时下降到 50% 以下,然后在时间 = 4 时高于并在时间 = 7 时低于,然后在时间 8 时降级,在时间 12 时降到以下,等等。 ...然后它在上面持续了 2 秒,然后是 3 秒,然后是 4 秒,依此类推。 ...因此所需的最终结果是超过 50% 的最大时间,在这种情况下为 4 秒(根据下面的数据帧部分)。 所以我需要将 thje 值 4 分配给 max(maxGradeTimePeriod) 请问这个简单的代码?提前致谢。

这是我迄今为止尝试过的(许多尝试中的 1 次!):

    maxGradeTimePeriod <- c()
    i <- 1

    while (i <= nrow(df)) {
            if (0.5 <= df$Grade[i]) {
                    p <- i+1
                    k <- p
                    while (k < (nrow(df)-1)) {
                            if (df$Grade[k] < 0.5) {
                                    time <- (df$Time[k-1])-df$Time[i]
                                    print(time)
                                    maxGradeTimePeriod <- append(maxGradeTimePeriod, time)
                            }
                            else {
                                    time <- max(df$Time)-df$Time[i]
                                    maxGradeTimePeriod <- append(maxGradeTimePeriod, time)
                                                                            }
                            k <- k+1
                            }
                    }
                    i <- i+1
            }
            else {
                    i <- i+1
            }
    }

样本数据框:

 time grade
    1   0.5
    2   0.5
    3   0.1
    4   0.5
    5   0.5
    6   0.5
    7   0.1
    8   0.5
    9   0.5
   10   0.5
   11   0.5
   12   0.1
   13   0.5
   14   0.5
   15   0.5
   16   0.1
   17   0.5
   18   0.5
   19   0.1
   20   0.5

【问题讨论】:

  • 请附上您的数据框样本
  • 我阅读了您的信息,但不明白您想要获得什么。 maxGradeTimePeriod?别的东西?给出输入数据的样本,并为其正确输出。
  • 欢迎来到 StackOverflow。请花时间阅读how to provide a great R example 上的这篇文章以及如何提供minimal, complete, and verifiable example 并相应地修改您的问题。 how to ask a good question 上的这些提示也可能有用。
  • 你能不能也给你的数据框的标题?此外,脚本也会出错。
  • while 内的else 中似乎多了一个右括号

标签: r loops nested-loops


【解决方案1】:

假设每一行都是一个时间单位,试试:

y <- rle(x$grade >= 0.5)              # Find clusters of values not below 0.5
max(y$length[which(y$values)])        # Find which TRUE cluster is the largest
# [1] 4

可能是时间点的间距不均匀。在这种情况下,请尝试:

x2 <- rle(x$grade >= 0.5)$length      # Get all clusters
x3 <- rep(seq_along(x2), x2)          # Make a vector that specifies cluster per value
time <- c(0, diff(x$time))            # Make a new vector with time differences 
y <- aggregate(time ~ x3, FUN=sum)    # Aggregate the sum of time per cluster
max(y$time)                           # Take the max
# [1] 4

【讨论】:

  • 稍后谢谢,我以前从未见过 rle 函数,用它来破解它,这应该很有趣 = 长度吗?
  • @wade12 这取决于。这是您的问题中唯一不清楚的事情。每行是 1 个时间单位(即您只需要 grade 向量)吗?因为如果是这样,你可以用更少的代码解决这个问题:)
  • 标题:时间、lav、noi、等级、战争。
  • @wade12:我根据这个新信息更新了脚本(即每一行是 1 个时间点)。如果这回答了您的问题,请按答案旁边的复选标记将问题标记为已回答。
  • 我将以你的名字命名我的第一个孩子!非常感谢。
猜你喜欢
  • 2018-12-21
  • 1970-01-01
  • 2020-05-11
  • 2019-12-30
  • 1970-01-01
  • 2018-05-16
  • 2023-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多