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