cRazyMind
cRazyMind

Reputation: 25

Excluding rows containting consecutive zeros from data frame

I would like to exclude rows with at least 2 consecutive zeros from my data frame. Example:

subj    stimulus    var1   var2   var3   var4   
 1         A         25     30     15     36
 1         B          0      0     10      0
 1         C         12      0     20     23

I would like to exclude the trial for stimulus B containing two zeros, but keep the values for C containing only one. So far I have tried:

has3Zeros <- function(x) {
  x <- x[!is.na(x)] == 0 
  if (length(x) < 3) {
    FALSE 
  }
}
df[,!sapply(df, has3Zeros), drop=F]

but it tells me it is an invalid argument type. Is there an "easy" way to accomplish excluding rows with consecutive zeros?

Thank you in advance.

Upvotes: 1

Views: 287

Answers (1)

akrun
akrun

Reputation: 887098

If we are looking for any consecutive zeros in each row and want to exclude that row, one way would be to loop through the rows using apply and MARGIN=1. Check whether there are any of the adjacent elements are equal and are zero, do the negation and subset the rows.

df1[!apply(df1[-(1:2)], 1, FUN = function(x) any((c(FALSE, x[-1]==x[-length(x)])) & !x)),]
#  subj stimulus var1 var2 var3 var4
#1    1        A   25   30   15   36
#3    1        C   12    0   20   23

Or if we need consecutive zero length to be 'n', then rle can be applied to each row, check whether the lengths for 'values' that are 0 is 'n', negate and subset the rows.

df1[!apply(df1[-(1:2)], 1, FUN = function(x) any(with(rle(x==0), lengths[values])==2)),]
#  subj stimulus var1 var2 var3 var4
#1    1        A   25   30   15   36
#3    1        C   12    0   20   23

Upvotes: 2

Related Questions