EMiller
EMiller

Reputation: 31

Replace negative values with NA if values occur x number of times in a row in R

I would like to replace negative values in a column with NA values if they occur more than 144 times back to back (aka in a row)in R. I've created a separate column that lists whether the value is negative by:

df$a_neg <- df$a < 0

This gives me a new column that says TRUE if the value was negative and FALSE if it was positive.

I've tried subsetting into a new dataframe by doing:

df_neg <- df %>%
subset() %>%
group_by(a_neg) %>%
filter(n() > 144)

All that does is give me back the exact frame dataframe as I started with. Any help will be greatly appreciated!

Upvotes: 2

Views: 90

Answers (1)

Sam Dickson
Sam Dickson

Reputation: 5249

You can try this:

# Create data
set.seed(123)
df = data.frame(a=rnorm(2000,-2.5))

# Find contiguous blocks of positive and negative values
df$block = cumsum(c(1,abs(diff(df$a<0))))

# Count how many negative values are in each block
df$block_neg = ave(df$a,df$block,FUN = function(x) {sum(x<0)})

# Assign NA to blocks with greater than 144 negative values
df$a1 = ifelse(df$block_neg>144,NA,df$a)

Upvotes: 1

Related Questions