【问题标题】:R: Split a Vector into Some Overlapping Sub-vectors with Its First Element being ReboundedR:将向量拆分为一些重叠的子向量,其第一个元素被反弹
【发布时间】:2021-11-21 23:17:24
【问题描述】:

我想拆分一个父子向量,使这些条件成立:

  1. 每个子向量都具有相等且恒定的长度l
  2. 子向量与l -1 相互重叠
  3. 子向量的数量为length(ts) - l + 2
  4. 最后一个子向量应该包含父向量的第一个元素作为它的最后一个子元素。

.

ts <- 1:11 # the parent vector
l <- 7 # constant length of sub-vectors to be
m <- length(ts) - l + 1 # number of sub-vector to be
split(t(embed(ts, m))[m:1,], 1:m) 

当我尝试这个(使用m &lt;- length(ts) - l + 1)时,我得到了我想要的l = 7,但得到了m = 5,而不是我想要的m = 6

#$`1`
#[1] 1 2 3 4 5 6 7

#$`2`
#[1] 2 3 4 5 6 7 8

#$`3`
#[1] 3 4 5 6 7 8 9

#$`4`
#[1]  4  5  6  7  8  9 10

#$`5`
#[1]  5  6  7  8  9 10 11

当我尝试这个(使用m &lt;- length(ts) - l + 2)时,我得到了我想要的m = 6,但l = 6而不是l = 7

ts <- 1:11 # the parent vector
l <- 7 # constant length of sub-vectors to be
m <- length(ts) - l + 2 # number of sub-vector to be
split(t(embed(ts, m))[m:1,], 1:m) 

这就是我得到的

#$`1`
#[1] 1 2 3 4 5 6

#$`2`
#[1] 2 3 4 5 6 7

#$`3`
#[1] 3 4 5 6 7 8

#$`4`
#[1] 4 5 6 7 8 9

#$`5`
#[1]  5  6  7  8  9 10

#$`6`
#[1]  6  7  8  9 10 11

我想要什么

#$`1`
#[1] 1 2 3 4 5 6 7

#$`2`
#[1] 2 3 4 5 6 7 8

#$`3`
#[1] 3 4 5 6 7 8 9

#$`4`
#[1]  4  5  6  7  8  9 10

#$`5`
#[1]  5  6  7  8  9 10 11

#$`6`
#[1]  6  7  8  9 10 11  1

【问题讨论】:

    标签: r vector split


    【解决方案1】:

    用适当的维度、索引和拆分填充矩阵

    asplit(
      matrix(ts, nrow = length(ts) + 1, ncol = length(ts) - l + 1)[1:(l + 1), ],
      MARGIN = 2)
    
    #[[1]]
    #[1] 1 2 3 4 5 6 7
    #
    #[[2]]
    #[1] 2 3 4 5 6 7 8
    #
    #[[3]]
    #[1] 3 4 5 6 7 8 9
    #
    #[[4]]
    #[1]  4  5  6  7  8  9 10
    #
    #[[5]]
    #[1]  5  6  7  8  9 10 11
    #
    #[[6]]
    #[1]  6  7  8  9 10 11  1
    

    【讨论】:

      【解决方案2】:

      如果它只适用于包含从 1 开始的整数序列的父向量(如 1:101:8),那么应该这样做:

      ts <- 1:11
      l <- 7
      m <- length(ts) - l + 2
      
      
      lapply(1:m, function(x) {
        y <- x:(x+l-1)
        y <- ifelse(y>max(ts), y-max(ts), y)} #here you make sure that 12 becomes 1 etc.
             )
      

      但如果您希望它适用于任何类型的原子向量(例如字母 A、B、C...K),请执行以下操作:

      ts2 <- LETTERS[1:11]
      l <- 7
      m <- length(ts) - l + 2
      
      # the output from the code above is stored to be used as index
      idx <- lapply(1:m, function(x) {
        y <- x:(x+l-1)
        y <- ifelse(y>length(ts2), y-length(ts2), y)}
      )
      
      # apply index to the parent vector
      lapply(idx, function(x) ts2[x])
      
      

      【讨论】:

        【解决方案3】:

        我会使用filter:

        ts <- 1:11 
        l <- 7
        
        lapply(seq_len(length(ts) - l + 2), function(i) {
          p <- rep(0, l)
          p[i] <- 1
          res <- c(stats::filter(rev(ts), filter = p, 
                                 circular = TRUE, sides = 1))
          rev(res)[seq_len(l)]
        })
        #[[1]]
        #[1] 1 2 3 4 5 6 7
        #
        #[[2]]
        #[1] 2 3 4 5 6 7 8
        #
        #[[3]]
        #[1] 3 4 5 6 7 8 9
        #
        #[[4]]
        #[1]  4  5  6  7  8  9 10
        #
        #[[5]]
        #[1]  5  6  7  8  9 10 11
        #
        #[[6]]
        #[1]  6  7  8  9 10 11  1
        

        【讨论】:

          【解决方案4】:

          给你:

          ts <- 1:11 # the parent vector
          l <- 7 # constant length of sub-vectors to be
          m <- length(ts) - l + 2 # number of sub-vector to be
          
          library(magrittr)
          lapply(1:m, function(i){
            c(0, rep(ts, 4)) %>% # Adding leading zero (cut in first iter; repeat a few times to not run out if we increase m
              tail(-i) %>% # Cut first i elements (including added zero)
              head(l) # Retain first l of the remaining part
          })
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-11-18
            • 1970-01-01
            • 2021-09-27
            • 2021-09-15
            • 2018-08-09
            • 1970-01-01
            • 2019-10-04
            • 1970-01-01
            相关资源
            最近更新 更多