I have a data frame with few thousands rows and selected 2 columns such as:
col1 col2
2 11
3 11
4 12
4 1
5 1
6 2
1 3
1 3
2 4
In each column values at some point reset to 1, and then go on acummulating up to some value before resetting again. Reset point in each column is independent from the other. What I need is a function detecting reset and updating values BEFORE this reset with negative values from -1 to -3 - for each column depending on its own reset. So needed result would be:
col1 col2
2 -3
3 -2
4 -1
-3 1
-2 1
-1 2
1 3
1 3
2 4
Any suggestions how this could be done? (Dplyr solution would be most welcome).
Another base R solution:
mydf[] <- lapply(mydf, function(x) {
w <- which(x == 1 & c(0, head(x,-1)) != 1)
x[c(sapply(w, `-`, 3:1))] <- -3:-1
x
})
which gives:
> mydf col1 col2 1 2 -3 2 3 -2 3 4 -1 4 -3 1 5 -2 1 6 -1 2 7 1 3 8 1 3 9 2 4
Old answer:
mydf[] <- lapply(mydf, function(x) {
w <- which(x == 1)
i <- c(0, diff(w)) != 1
w <- c(sapply(w[i], `-`, 3:1))
x[w] <- -3:-1
x
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With