0

我正在尝试创建一个“过滤依据”矩阵,我可以用它来隔离我的数据框中的数据行,这样每一行只包含对应于相同数字的最长连续序列的值,而其余的保持为零。搜索后,我认为 rle 是要使用的功能,但这并没有给我我所追求的。这是我的代码和结果的示例。建议和解决方案将不胜感激。谢谢!

样本数据:

    a<- c(1,0,1,1,1,1,0,0)
    b<- c(0,0,0,1,1,1,0,1)
    c<- c(0,0,1,1,0,0,0,1)
    d<- c(1,0,0,1,1,1,1,0)
    e<- c(1,0,0,1,0,0,1,1)
    f<- c(0,0,0,1,1,1,0,1)
    g<- c(0,0,1,1,0,0,0,1)
    test.data <- data.frame(cbind(a,b,c,d,e,f,g))

    # > test.data
    #   a b c d e f g
    # 1 1 0 0 1 1 0 0
    # 2 0 0 0 0 0 0 0
    # 3 1 0 1 0 0 0 1
    # 4 1 1 1 1 1 1 1
    # 5 1 1 0 1 0 1 0
    # 6 1 1 0 1 0 1 0
    # 7 0 0 0 1 1 0 0
    # 8 0 1 1 0 1 1 1

尝试解决方案的示例代码:

result <- data.frame(lapply(test.data, function(x) {
  r <- rle(x)
  r$values[r$lengths!=max(r$lengths)]==1
  r2=inverse.rle(r)
  r2
}))

结果我得到(看起来像里面的精确副本?):

# > result
#    a b c d e f g
# 1  1 0 0 1 1 0 0
# 2  0 0 0 0 0 0 0
# 3  1 0 1 0 0 0 1
# 4  1 1 1 1 1 1 1
# 5  1 1 0 1 0 1 0
# 6  1 1 0 1 0 1 0
# 7  0 0 0 1 1 0 0
# 8  0 1 1 0 1 1 1

这是我想要得到的结果(如果更容易,可以使用 T/F 代替 1 和 0):

# > result
#    a b c d e f g
# 1  0 0 0 1 1 0 0
# 2  0 0 0 0 0 0 0
# 3  0 0 0 0 0 0 0
# 4  1 1 1 1 1 1 1
# 5  1 1 0 0 0 0 0
# 6  1 1 0 0 0 0 0
# 7  0 0 0 1 1 0 0
# 8  0 0 0 0 1 1 1

请指教!

4

2 回答 2

0
library(magrittr)

val <- 1

test.data %>% 
    apply(1, function(x){
      rle(x) %$% { 
        if(all(values != val)) rep(0, length(x))
        else {
          m      <- max(lengths[values == val]) 
          # Get only longest sequences
          values <- (lengths == m & values == val)*values*(m > 1)
          # Get only one of them
          values[seq_along(values) != which(values == val)[1]] <- 0
          rep(values, lengths)
        }
    }}) %>% t

#      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,]    0    0    0    1    1    0    0
# [2,]    0    0    0    0    0    0    0
# [3,]    0    0    0    0    0    0    0
# [4,]    1    1    1    1    1    1    1
# [5,]    1    1    0    0    0    0    0
# [6,]    1    1    0    0    0    0    0
# [7,]    0    0    0    1    1    0    0
# [8,]    0    0    0    0    1    1    1
于 2018-06-08T19:35:08.663 回答
0

我想这就是你所追求的...

test.data[] <- t(apply(test.data,1,function(x) {y<-rle(x)
           y$values[y$lengths==1] <- 0
           y$values[y$lengths!=max(y$lengths[y$values==1])] <- 0
           return(inverse.rle(y))}))

test.data
  a b c d e f g
1 0 0 0 1 1 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 1 1 1 1 1 1 1
5 1 1 0 0 0 0 0
6 1 1 0 0 0 0 0
7 0 0 0 1 1 0 0
8 0 0 0 0 1 1 1
于 2018-06-08T19:28:32.367 回答