psycho95
psycho95

Reputation: 141

R create loop for comparing rows

I got a dataframe like this:

  id      day  time state 
  <chr> <dbl> <dbl> <dbl> 
1 A         1     1     0     
2 A         1     2     0     
3 A         1     3     1     
4 A         2     1     0     
5 A         2     2     1     
6 A         2     3     1     
7 A         3     1     1     
8 A         3     2     1     
9 A         3     3     0     

In the original dataframe, there are 30 ids and every id has 5 days with 1440 timepoints (so 216000 rows in total).

Now I want to create a new variable called "delta" as result for comparing if the state (1 or 0) is equal between two different timeponts (1 = equal, 0 = unequal).

For example: Compare if state of day 1 time 1 is = state of day 2 time 1, day 1 time 2 = day 2 time 2.... and then day 2 time 1 = day 3 time 1 and so on. In the end it should look like this:

 id      day  time state delta
  <chr> <dbl> <dbl> <dbl> <dbl>
1 A         1     1     0     1
2 A         1     2     0     0
3 A         1     3     1     1
4 A         2     1     0     0
5 A         2     2     1     1
6 A         2     3     1     0
7 A         3     1     1     1
8 A         3     2     1     0
9 A         3     3     0     1

I already tried some codes with the ifelse-command, but I could work it out yet.

Upvotes: 0

Views: 44

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145765

Assuming my assumption here is correct: you want for the day i time j row, the delta value compares if the state on day i + 1 time j (next day, same time) is equal.

Here's a dplyr method:

library(dplyr)
your_data %>% 
  group_by(time) %>%
  arrange(day) %>%
  mutate(delta = as.integer(lead(state) == state))

Upvotes: 2

Related Questions