This is the input:
book1 <- structure(
list(
ID = 1:10,
X1 = c(123L, 434L, 545L, 2323L, 54L,
23L, 54L, 24L, 53L, 44L),
X2 = c(132L, 44L, 543L, 335L, 53L,
33L, 42L, 24L, 35L, 55L),
X3 = c(12L, 143L, 533L, 344L, 232L,
5332L, 23L, 333L, 56L, 75L),
X4 = c(123L, 434L, 545L, 2323L,
54L, 23L, 54L, 24L, 53L, 44L),
X5 = c(132L, 44L, 543L, 335L,
53L, 33L, 42L, 24L, 35L, 55L),
X6 = c(12L, 143L, 533L, 344L,
232L, 5332L, 23L, 333L, 56L, 75L),
X7 = c(123L, 434L, 545L, 2323L,
54L, 23L, 54L, 24L, 53L, 44L),
X8 = c(132L, 44L, 543L, 335L,
53L, 33L, 42L, 24L, 35L, 55L),
X9 = c(12L, 143L, 533L, 344L,
232L, 5332L, 23L, 333L, 56L, 75L),
X10 = c(123L, 434L, 545L,
2323L, 54L, 23L, 54L, 24L, 53L, 44L),
X11 = c(132L, 44L, 543L,
335L, 53L, 33L, 42L, 24L, 35L, 55L),
X12 = c(12L, 143L, 533L,
344L, 232L, 5332L, 23L, 333L, 56L, 75L)
),
class = "data.frame",
row.names = c(NA,-10L)
)
And this is the input I'm expecting: expected result
I think I should use for loop and the lag function, but can't get it to work. I want the first stays the same, and get the difference starting the second row.