Tareva
Tareva

Reputation: 230

Replacing row values in R based on previous rows

Below is my data frame df

df <- data.frame(A=c(1,1,1,1,0,0,-1,-1,-1,1,1,1,1))

I would like to have another variable T_D which maintains the first value when it encounters the change in the value of A by either 1 or -1and replaces the next rows by 0

Expected Output:

A  T_D
1  1
1  0
1  0
1  0
0  0 
0  0
-1 -1
-1  0
-1  0 
1  1
1  0  
1  0
1  0   

Upvotes: 3

Views: 2547

Answers (4)

Sotos
Sotos

Reputation: 51582

A data.table approach would be,

library(data.table)
setDT(df)[, T_D := replace(A, duplicated(A), 0), by = rleid(A)][]

#     A T_D
# 1:  1   1
# 2:  1   0
# 3:  1   0
# 4:  1   0
# 5:  0   0
# 6:  0   0
# 7: -1  -1
# 8: -1   0
# 9: -1   0
#10:  1   1
#11:  1   0
#12:  1   0
#13:  1   0

Upvotes: 1

Andrew Brēza
Andrew Brēza

Reputation: 8317

dplyr's window functions make this easy. You can use the lag function to look at the previous value and see if it equals the current value. The first row of the table doesn't have a previous value so T_D will always be NA. Fortunately that row will always be equal to a so it's an easy problem to fix with a second mutate (or df[1,2] <- df[1,1]).

library(tidyverse) # Loads dplyr and other useful packages

df <- tibble(a = c(1, 1, 1, 1, 0, 0, -1, -1, -1, 1, 1, 1, 1))

df %>%
  mutate(T_D = ifelse(a == lag(a), 0, a)) %>%
  mutate(T_D = ifelse(is.na(T_D), a, T_D))

Upvotes: 2

Miroslav Radojević
Miroslav Radojević

Reputation: 517

df$T_D <- sign(abs(df$A)*diff(c(0, df$A)))

Upvotes: 1

Florian
Florian

Reputation: 25375

Base R solution, this seems to work for you:

df$T_D = df$A*!c(FALSE,diff(df$A,lag=1)==0),

Find the difference between sequential rows. If the difference is 1, take the entry from column A, otherwise set to 0.

OUTPUT

    A T_D
1   1   1
2   1   0
3   1   0
4   1   0
5   0   0
6   0   0
7  -1  -1
8  -1   0
9  -1   0
10  1   1
11  1   0
12  1   0
13  1   0

Upvotes: 1

Related Questions