【问题标题】:Calculate maximum length of consecutive values in row over a set number计算一组连续值的最大长度
【发布时间】:2021-08-12 14:26:23
【问题描述】:

我有一个随机的 10x10 DF(实际上它有几百万行):

df <- replicate(10, sample(0:5, 10, rep=T))

我需要在我的 df 末尾计算一个列,该列是连续值的最大长度等于或超过一个设定值的计数,例如3 个或更多。

因此,包含以下值的单行:2,4,3,3,4,5,1,0,5,1 将返回值 5,作为值 4,3,3 的集合, 4,5都是3个或更多,并且是连续的。

虽然 5 确实在高于 3 的行中再次出现,但其连续出现少于 5 个连续数字,超过 3 的行中较早。

任何帮助表示赞赏。

【问题讨论】:

    标签: r


    【解决方案1】:
    # condition is that x should be larger or equal to 3
    condition <- function(x) x >= 3
    
    # example row
    row = c(2,4,3,3,4,5,1,0,5,1)
    
    # we can use condition on row:
    condition(row)
    
    # and we can emplay rle on that:
    rle(condition(row))
    
    # we need to filter those rle results for TRUE:
    r <- rle(condition(row))
    r$length[r$values == TRUE]
    
    # The answer is the max of the latter
    max(r$length[r$values])
    

    或用于您的数据框示例

    # condition is that x should be larger or equal to 3
    condition <- \(x) x >= 3
    
     
    number <- function(row, condition){
      r <- row |>
             condition() |>
             rle()
      max(r$length[r$values])
    }
    
    df <- replicate(10, sample(0:5, 10, rep=T))
    apply(df, 1, number, condition)
    

    【讨论】:

      【解决方案2】:

      在此处使用rle 进行行程编码。

      vec <- c(2,4,3,3,4,5,1,0,5,1)
      r <- rle(vec >= 3)
      r
      # Run Length Encoding
      #   lengths: int [1:5] 1 5 2 1 1
      #   values : logi [1:5] FALSE TRUE FALSE TRUE FALSE
      ind <- head(which(r$values), 1)
      ind
      # [1] 2
      r$lengths[ind]
      # [1] 5
      
      ### to see what those five values are ...
      r$values[-ind] <- FALSE
      vec[inverse.rle(r)]
      # [1] 4 3 3 4 5
      

      这使我们获得了该行中最长的长度。要将此逐行应用于帧,

      set.seed(42)
      df <- replicate(10, sample(0:5, 10, rep=T))
      df
      #       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
      #  [1,]    0    0    3    1    3    5    1    3    5     3
      #  [2,]    4    4    4    5    4    5    4    0    1     1
      #  [3,]    0    5    4    2    3    1    0    2    1     1
      #  [4,]    0    3    4    5    1    3    0    2    0     2
      #  [5,]    1    1    3    1    1    2    3    4    1     4
      #  [6,]    3    1    1    3    2    5    4    4    4     4
      #  [7,]    1    2    3    3    0    4    1    3    5     5
      #  [8,]    1    0    2    5    4    1    0    5    4     2
      #  [9,]    0    0    1    1    1    5    4    4    3     5
      # [10,]    3    2    0    4    1    1    3    3    0     3
      
      func <- function(x, lim = 3) {
        r <- rle(x >= lim)
        ind <- head(which(r$values), 1)
        if (length(ind) == 1 && !anyNA(ind)) r$lengths[ind] else 0
      }
      apply(df, 1, func)
      #  [1] 1 7 2 3 1 1 2 2 5 1
      

      【讨论】:

        猜你喜欢
        • 2021-05-08
        • 1970-01-01
        • 2021-09-25
        • 2019-05-09
        • 2019-10-13
        • 2021-05-26
        • 2016-11-04
        • 1970-01-01
        • 2019-04-11
        相关资源
        最近更新 更多