0

I'm trying to add an indicator column to my data frame:

  • if the two previous values are positive, the result is 1;
  • if they're negative, -1
  • otherwise 0.

Here's an example:

a <- data.frame(col = c(-1,-2, 3, 4, 0))
#function should return a data frame
b <- function(a)
print(b)
(NA, NA, -1, 0, 1)
IJH
  • 167
  • 1
  • 11
  • @ZheyuanLi my data frame has seven numeric columns with values both positive and negative. I would like to do the aforementioned operation on three of the seven – IJH Jul 05 '16 at 18:16
  • If my data frame had five rows with the following values: `(-1,-2,3,4,0)`. The function I'm trying to build would return `(NA, NA, -1, 0, 1)` – IJH Jul 05 '16 at 18:19
  • I used a vector as an example, as it was easier to write within stackoverflow. I'm using columns of a data frame (which obviously can be turned into vectors). The answer below does what I wanted the function to do – IJH Jul 05 '16 at 18:24
  • Thank you for your help. If there is a better way to display code/output, I'd love to learn – IJH Jul 05 '16 at 18:28
  • @IJH [here's](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) a frequently linked discussion of making a reproducible example – bouncyball Jul 05 '16 at 18:54

1 Answers1

2

Here's a quick demonstration using a numeric vector with 20 entries:

set.seed(123)
x = rnorm(20)
x_res = c(NA, NA,  
sapply(3:20, FUN = function(y) 
sign(x[y-1]) * as.numeric(sign(x[y-1]) == sign(x[y-2]))))
DF = data.frame(x, x_res)
DF
             x x_res
1  -0.56047565    NA
2  -0.23017749    NA
3   1.55870831    -1
4   0.07050839     0
5   0.12928774     1
6   1.71506499     1
7   0.46091621     1
8  -1.26506123     1
9  -0.68685285     0
10 -0.44566197    -1
11  1.22408180    -1
12  0.35981383     0
13  0.40077145     1
14  0.11068272     1
15 -0.55584113     1
16  1.78691314     0
17  0.49785048     0
18 -1.96661716     1
19  0.70135590     0
20 -0.47279141     0

Here's a wrapper function that might make this a bit more versatile (assuming your lag will always be 2):

create_lag_column <- function(x){
    c(NA, NA, 
      sapply(3:length(x), FUN = 
      function(y) sign(x[y-1]) * as.numeric(sign(x[y-1]) == sign(x[y-2])))
    )
}
bouncyball
  • 10,631
  • 19
  • 31