let's have data frame follows:
bid diff 1 1 na 2 1 0.0 3 1 0.0 4 1 -0.5 5 1 0.0 6 1 0.0 7 1 0.0 8 1 0.5 9 1 0.0 10 1 0.0 11 2 na 12 2 0.0 13 2 0.0 14 2 0.0 15 2 -0.5 16 2 0.0 17 2 0.0 18 2 0.0 19 2 0.0 20 3 na 21 3 0.5 22 3 0.0 23 3 -0.5 24 3 0.5
what is, each bid
, create new variable neg
equal 0 before negative diff
value , equal 1 after negative diff
value. should stop when diff
changes again. e.g.
bid diff neg 1 1 na 0 2 1 0.0 0 3 1 0.0 0 4 1 -0.5 na 5 1 0.0 1 6 1 0.0 1 7 1 0.0 1 8 1 0.5 na 9 1 0.0 na 10 1 0.0 na 11 2 na 0 12 2 0.0 0 13 2 0.0 0 14 2 0.0 0 15 2 -0.5 na 16 2 0.0 1 17 2 0.0 1 18 2 0.0 1 19 2 0.0 1 20 3 na na 21 3 0.5 na 22 3 0.0 0 23 3 -0.5 na 24 3 0.5 na
i tried approaching running '1's dplyr
follows:
data <- data %>% group_by(bid) %>% mutate(neg = as.numeric( ifelse(lag(diff) == -0.5, 1, ifelse(((lag(neg) == 1) & (diff == 0.0)), lag(neg), 0))))
i guess it's obvious works if 2 or fewer 1's needed following -0.5 in diff
.
any appreciated, , open other methods besides dplyr
. data time series , diff
difference between variable's current , lagged value each bid. let me know if other information may helpful.
this not efficient way it, here go:
df <- read.table(text = ' bid diff 1 1 na 2 1 0.0 3 1 0.0 4 1 -0.5 5 1 0.0 6 1 0.0 7 1 0.0 8 1 0.5 9 1 0.0 10 1 0.0 11 2 na 12 2 0.0 13 2 0.0 14 2 0.0 15 2 -0.5 16 2 0.0 17 2 0.0 18 2 0.0 19 2 0.0 20 3 na 21 3 0.5 22 3 0.0 23 3 -0.5 24 3 0.5', header = false) df[is.na(df)] <- 0 df$neg <- 0 (i in 1:length(df$diff)) { flag <- ifelse(df$diff[i] < 0, 1, 0) if (flag == 1) { (j in i:length(df$diff)) { if (df$diff[j] > 0) { flag <- 0 break } else { df$neg[j] <- 1 } } } } df$neg[df$diff < 0] <- 0 df ## bid diff neg ## 1 1 0.0 0 ## 2 1 0.0 0 ## 3 1 0.0 0 ## 4 1 -0.5 0 ## 5 1 0.0 1 ## 6 1 0.0 1 ## 7 1 0.0 1 ## 8 1 0.5 0 ## 9 1 0.0 0 ## 10 1 0.0 0 ## 11 2 0.0 0 ## 12 2 0.0 0 ## 13 2 0.0 0 ## 14 2 0.0 0 ## 15 2 -0.5 0 ## 16 2 0.0 1 ## 17 2 0.0 1 ## 18 2 0.0 1 ## 19 2 0.0 1 ## 20 3 0.0 1 ## 21 3 0.5 0 ## 22 3 0.0 0 ## 23 3 -0.5 0 ## 24 3 0.5 0
what's happening is: everytime finds negative number in diff
, sets flag, , changes following values 1, till finds positive number.
Comments
Post a Comment