Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional increment tidyverse

Tags:

r

dplyr

I've been googling quite unsuccessfully how to increment conditionally in tidyverse. What I want to do is check if value in a column is greater than some x, and if so increment an integer by one. Every observation starts with 1.

Example code:

id = c(1, 1, 1, 2, 3, 3, 3, 3, 4)
time = c(20, 30, 101, 33, 50, 101, 30, 110, 30)

df_x = data.frame(id = id, time = time)

Output:

  id time
1  1   20
2  1   30
3  1  101
4  2   33
5  3   50
6  3  101
7  3   30
8  3  110
9  4   30

Desired output:

increment = c(1, 1, 2, 1, 1, 2, 2, 3, 1)

df_x$increment = increment

   id time increment
1  1   20         1
2  1   30         1
3  1  101         2
4  2   33         1
5  3   50         1
6  3  101         2 
7  3   30         2
8  3  110         3
9  4   30         1

The code for it would be something like:

df_x %>%
  group_by(id) %>%
  mutate(ifelse(time <= 100, ?, ?))

Any help will be greatly appreciated.

like image 513
docjay Avatar asked Jun 27 '18 11:06

docjay


1 Answers

This can be done using a cumulative sum, which increments each time the value is greater than 100, for example:

df_x %>% 
  group_by(id) %>% 
  mutate(increment = 1 + cumsum(time > 100))

# A tibble: 9 x 3
# Groups:   id [4]
     id  time increment
  <dbl> <dbl>     <dbl>
1    1.   20.        1.
2    1.   30.        1.
3    1.  101.        2.
4    2.   33.        1.
5    3.   50.        1.
6    3.  101.        2.
7    3.   30.        2.
8    3.  110.        3.
9    4.   30.        1.

I used 1 + cumsum(...) in order to start the first group from 1 instad of 0. Not that a group might start with a 2 if the first value is >100 in a given id-group.

like image 139
talat Avatar answered Oct 30 '22 04:10

talat