So, the problem is, I'm trying to get the first smaller value within the top rows of the transformed variable. My df looks something like:
count |
---|
24 |
33 |
33 |
34 |
35 |
33 |
34 |
35 |
... |
It only contains a column. The output that I'm looking for:
count | close_prev |
---|---|
24 | NA |
33 | 24 |
33 | 24 |
34 | 33 |
35 | 34 |
33 | 24 |
33 | 24 |
34 | 33 |
35 | 34 |
So, I'm looking for the first smaller number from the top rows.
The code I have so far:
table %>%
mutate(close_prev = map_dbl(row_number(), ~closest(count[seq_len(max(.x - 1, 1))], count[.x])))
It's not working :c Can someone help me? Thank you in advance