Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove data.frame rows containing zero, where adjacent rows contain zero

Tags:

r

I would like to remove all rows that contain a zero, but only if (the row below it is zero AND the row above it is also zero) OR (it is a zero AND it is the first observation AND the number below it is also a zero).

For example:

RowNumb   Column2  
  1          0  
  2          0  
  3          0  
  4          1   
  5          0  
  6          1    
  7          1  
  8          0  
  9          0   
  10         0

I would like to remove rows 1, 2, 9 and 10, as these are the only rows that are equal to zero, with a zero below them and either a zero or nothing (in the case of rowNumb 1) above them, such that I get the following:

RowNumb   Column2  
  3          0  
  4          1   
  5          0  
  6          1    
  7          1  
  8          0  

Does anyone know of a way of doing this without using a loop?

like image 660
Mike Avatar asked Jun 12 '26 22:06

Mike


1 Answers

You can use filter to sum each absolute value with the preceding and following absolute value and compare that sum with 0:

DF <- read.table(text="RowNumb   Column2  
  1          0  
  2          0  
  3          0  
  4          1   
  5          0  
  6          1    
  7          1  
  8          0  
  9          0   
  10         0", header=TRUE)

rem <- na.omit(filter(abs(c(0, DF$Column2, 0)), rep(1, 3)) != 0L)

DF[rem,]
#  RowNumb Column2
#3       3       0
#4       4       1
#5       5       0
#6       6       1
#7       7       1
#8       8       0

This assumes there are no NA values. If those can occur you'd need to modify this a bit:

x <- c(0, DF$Column2, 0)         
rem <- na.omit(filter(x != 0L | is.na(x) , rep(1, 3)) != 0L)
like image 153
Roland Avatar answered Jun 15 '26 11:06

Roland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!