Conditional row removal based on number of NA's within the row

拥有回忆 提交于 2019-12-05 18:03:45

Both conditions at once:

data[!apply(is.na(data), 1, function(x) 
  {v <- cumsum(x); any(diff(v, 3) == 3) | 4 %in% v}), ]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,]    1    1    2    3    4    2    3    2
# [2,]   NA    1   NA    4    1    1   NA    2
# [3,]    1    4    6    7    3    1    2    2

any(diff(v, 3) == 3) is TRUE if there were NA three times in a row (so the difference somewhere is 3) and 4 %in% v corresponds to the second condition.

Not a beauty, but it'll work:

rle.na <- apply(is.na(data), 1, function(z){
  tmp <- rle(z)
  tmp$lengths[tmp$values]
})
data[!sapply(rle.na, function(z) any(z == 3)) | rowSums(is.na(data)) > 4, ]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!