Search code examples
rdplyrzoorollapply

How to count the number of negative values for each observation of a certain variable


I want to calculate total number of negative values for each observation, using previous 10 observations. I used the following code, but it does not work -

funda_addit <- funda_addit %>% 
  group_by(TICKER) %>%
  arrange(year) %>% 
  mutate(NEG_EARN = rollapply(ni, 10, sum (ni<0), partial=TRUE)) %>% 
  ungroup()

Actually I want to create the new variable "NEG_EARN", which is the number of negative values of previous 10 observations(10 years in my data) for the variable "ni". I also use the following code, but it does not work -

funda_addit <- funda_addit %>% 
  group_by(TICKER) %>%
  arrange(year) %>% 
  mutate(NEG_EARN = rollapply(ni, 10, length(which(ni<0)), partial=TRUE)) %>% 
  ungroup()

Solution

  • We can use anonymous function call (or create a new function) instead of the whole column 'ni'

    library(dplyr)
    library(zoo)
    f1 <- function(x) sum(x < 0)
    funda_addit %>% 
      group_by(TICKER) %>%
      arrange(year) %>% 
      mutate(NEG_EARN = rollapplyr(ni, 10, FUN = f1, partial=TRUE)) %>% 
      ungroup()
    

    EDIT: changed rollapply to rollapplyr (based on comments from @IceCreamToucan)

    data

    set.seed(24)
    funda_addit <- data.frame(TICKER = rep(LETTERS[1:3], each = 20), 
          year = 1921:1940, ni = rnorm(60))