【问题标题】:Create a list of vectors from a vector where n consecutive values are not 0 in R从 R 中 n 个连续值不为 0 的向量创建向量列表
【发布时间】:2021-06-03 06:49:23
【问题描述】:

所以我有这个向量:

a = sample(0:3, size=30, replace = T)
 [1] 0 1 3 3 0 1 1 1 3 3 2 1 1 3 0 2 1 1 2 0 1 1 3 2 2 3 0 1 3 2

我想要的是一个向量列表,其中包含由 n 个 0 分隔的所有元素。所以在这种情况下,n = 0(连续值之间不能有任何 0),这将给出:

res = c([1,3,3], [1,1,1,3,3,2,1,1,3], [2,1,1,2]....)

但是,如果我将其设置为 2,我想灵活地控制 n 参数,例如:

b = c(1,2,0,3,0,0,4)

仍然会产生这样的结果

res = c([1,2,3],[4])

在尝试计算 0 的数量时,我尝试了很多在 for 循环中使用 while 循环的方法。但我就是做不到。

更新

我尝试在此处以更真实的环境发布问题: Flexibly calculate column based on consecutive counts in another column in R

感谢大家的帮助。以我有限的知识,我似乎无法将您的帮助付诸实践..

【问题讨论】:

  • 为了能够复制您的sample 数据,请使用set.seed。也就是说,我认为你的向量'b'足以证明问题 - 只需在整个过程中使用它;)而且,每当使用“连续”这个词时,它闻起来rle。检查一下!
  • 这可能需要专注于特定的编程问题,您应该在 Stack Overflow 上添加您尝试成为 on-topic 的代码。

标签: r list for-loop while-loop


【解决方案1】:

这是一个基本 R 选项,在一般情况下使用rle + split,即b 中的值不限于03

with(
  rle(with(rle(b == 0), rep(values & lengths == n, lengths))),
  Map(
    function(x) x[x != 0],
    unname(split(b, cut(seq_along(b), c(0, cumsum(lengths))))[!values])
  )
)

给出(假设n=2

[[1]]
[1] 1 2 3

[[2]]
[1] 4

如果您在 ragne 09 中有值,您可以尝试下面的代码

lapply(
  unlist(strsplit(paste0(b, collapse = ""), strrep(0, n))),
  function(x) {
    as.numeric(
      unlist(strsplit(gsub("0", "", x), ""))
    )
  }
)

这也给了

[[1]]
[1] 1 2 3

[[2]]
[1] 4

【讨论】:

    【解决方案2】:

    我还想使用来自 DescTools 的函数 SplitAt 粘贴一个有用的解决方案:

    SplitAt(a, which(a==0)) %>% lapply(., function(x) x[which(x != 0)])
    

    其中 a 是您的初始向量。它为您提供了一个列表,其中每个条目都包含零之间的一对数字:

    如果您添加另一个带有空字符的 SplitAt,您可以在子列表之后创建子列表并将其拆分为任意数量的子列表:例如:

    n <- 4
    SplitAt(a, which(a==0)) %>% lapply(., function(x) x[which(x != 0)]) %>% SplitAt(., n)
    

    给你:

    【讨论】:

      【解决方案3】:
      set.seed(1)
      a <- sample(0:3, size=30, replace = T)
      a
      [1] 0 3 2 0 1 0 2 2 1 1 2 2 0 0 0 1 1 1 1 2 0 2 0 0 0 0 1 0 0 1
      
      a2 <- paste(a, collapse = "") # Turns into a character vector, making it easier to handle patterns.
      a3 <- unlist(strsplit(a2, "0")) # Change to whatever pattern you want, like "00".
      a3 <- a3[a3 != ""] # Remove empty elements
      a3 <- as.numeric(a3) # Turn back to numeric
      a3
      [1]     32      1 221122  11112      2      1      1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-05
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多