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

烈酒焚心 提交于 2019-12-07 07:49:35

问题


I am looking to remove rows from my dataset based on two conditions as follows:

  1. Remove row if 3 consecutive cells are NA or
  2. If four or more cells are NA

My sample data:

data <- rbind(c(1,1,2,3,4,2,3,2),
              c(NA,1, NA, 4,1,1,NA,2), 
              c(1,4,6,7,3,1,2,2), 
              c(NA,3, NA, 1,NA,2,NA,NA), 
              c(1,4, NA, NA,NA,4,3,2))

I have researched within the existing questions and found that na.omit or complete.cases can remove rows with NA but as I have conditions, doing further research I have found the following code within the existing questions:

data[! rowSums(is.na(data)) >4  , ]   
data[! rowSums(is.na(data)) ==3  , ]

The first line full fill my second condition. the second line does remove rows with three NA's but not looking for consecutive and removing any rows with total 3 NA's. for example:

> data
     [,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
[4,]   NA    3   NA    1   NA    2   NA   NA
[5,]    1    4   NA   NA   NA    4    3    2

> data[! rowSums(is.na(data)) ==3  , ]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    1    2    3    4    2    3    2
[2,]    1    4    6    7    3    1    2    2
[3,]   NA    3   NA    1   NA    2   NA   NA

What I actually want is the 5th row to be removed only as this has three consecutive NA's and not the 2nd row.

Could anyone please advice me how can I overcome this?


回答1:


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.




回答2:


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, ]


来源:https://stackoverflow.com/questions/15186697/conditional-row-removal-based-on-number-of-nas-within-the-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!