Say I have the following sample data:
iris <- data.table(iris)[c(1:5,51:55,101:105), list(ID=.I, Species,Sepal.Length)]
Then say that I want to calculate the absolute difference between the lines within the group (in this case Species ).
iris[ , SL.Diff := c(NA,abs(diff(Sepal.Length))) , by = Species]
At this point, I have a dataset that looks like this:
ID Species Sepal.Length SL.Diff 1: 1 setosa 5.1 NA 2: 2 setosa 4.9 0.2 3: 3 setosa 4.7 0.2 4: 4 setosa 4.6 0.1 5: 5 setosa 5.0 0.4 6: 6 versicolor 7.0 NA
Now I want to compute a new variable, Sepal.Length2 , which takes the value of the next line if SL.Diff less than a threshold of 0.3.
iris[ , Sepal.Length2 := ifelse(SL.Diff < 0.3, iris[ID+1]$Sepal.Length, Sepal.Length)]
It works the way I want it. But what if I want to do the same comparison, but instead of taking the next line, I want to take the value of the previous line?
iris[ , Sepal.Length3 := ifelse(SL.Diff < 0.3, iris[ID-1]$Sepal.Length, Sepal.Length)]
Sepal.Length3 does not give the result that I expected. Does anyone know what I can do wrong here?
ID Species Sepal.Length SL.Diff Sepal.Length2 Sepal.Length3 1: 1 setosa 5.1 NA NA NA 2: 2 setosa 4.9 0.2 4.7 4.9 3: 3 setosa 4.7 0.2 4.6 4.7 4: 4 setosa 4.6 0.1 5.0 4.6 5: 5 setosa 5.0 0.4 5.0 5.0 6: 6 versicolor 7.0 NA NA NA 7: 7 versicolor 6.4 0.6 6.4 6.4 8: 8 versicolor 6.9 0.5 6.9 6.9 9: 9 versicolor 5.5 1.4 5.5 5.5 10: 10 versicolor 6.5 1.0 6.5 6.5 11: 11 virginica 6.3 NA NA NA 12: 12 virginica 5.8 0.5 5.8 5.8 13: 13 virginica 7.1 1.3 7.1 7.1 14: 14 virginica 6.3 0.8 6.3 6.3 15: 15 virginica 6.5 0.2 NA 5.1