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