0

Using a one column data frame, I want to subtract the days value from the previous row and create a new column called interval, with the difference value. When I run the code I created, I get two additional columns. .index and .value.

I have tried multiple operators

deploy$interval <- getInterval(deploy$days[i], i, y)
deploy$interval = getInterval(deploy$days[i], i, y)
deploy <- data.frame(
  days = c(5,10,40,20,15,5,3,7,34,20,16,7,5,8,9,6,10,5,15,9,7,6,9,7,8,9,6,7,8,6,9,7,8,8,6,7,5,6,7,5,6,5,7,5,6,4,5,4,5,6,4,5,6,4,5,3,5)
)

getInterval <- function(x, i, c){
  if (i == 1) {
    y = 0
    z = x
  } else {
      y = x-z
      z = x
    }
  return (y)
}

y <- nrow(deploy)

for (i in 1:y) {
  deploy$interval <- getInterval(deploy$days[i], i, y)
}

print (deploy)

Got a .index and a .value column. Expected only interval column. btw, the interval.index column does contain the values I was looking for...

obs  days interval.index interval.value
1   5   0   -120
2   10  -5  -995
3   40  -35 -63995
4   20  -15 -7995
5   15  -10 -3370
6   5   0   -120

  • 1
    Your output is not repreducible, the code breaks when `i=2` because you're creating the object `z` inside of a function when `i=1`, but that object is not stored in your global environment. You should try to rework your for loop, but the easiest way to acomplish your goal is to use the `lag` function. See [here](https://stackoverflow.com/questions/30606360/subtract-value-from-previous-row-by-group) – astrofunkswag Sep 10 '19 at 20:19
  • 1) The function has an argument `c` that is never used. 2) To *"create a new column called interval, with the difference value"* see `diff`. Is `c(0, diff(deploy$days))` what you want? – Rui Barradas Sep 10 '19 at 20:25

0 Answers0