Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update rows depending on the value in following rows in multiple columns

Tags:

r

dplyr

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).

like image 998
Bartosz M Avatar asked Dec 02 '22 11:12

Bartosz M


1 Answers

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
})
like image 150
Jaap Avatar answered Jan 19 '23 11:01

Jaap