I have a dataframe where choices were made sequentially inside sessions. I would like to create a variable indicating the order number of every choice. The problem is that I know only what was the first choice in every session, and I want to know the order of every choice.
So let say we have a choice and a signal telling us if this was the first choice in a session a not. Let assume also that the data is ordered. What I would like is to obtain a third column (order) indicating the choice order, so that every time we have a 1, the order is 1, and it is going up (2, 3,...) until the next 1.
df = data.frame(
choice = c('a','a','b','e','a','l','d','a'),
signal = c(1,0,0,1,0,0,0,0),
order = c(1,2,3,1,2,3,4,5))
choice signal order
1 a 1 1
2 a 0 2
3 b 0 3
4 e 1 1
5 a 0 2
6 l 0 3
7 d 0 4
8 a 0 5
So I try to solve that with map, but it did not work for an obvious reason: I don't know how to update a vector outside of the map.
my_order = df$signal
map(
.x = seq(1,(df$signal %>% length())),
.f = function(x) {
my_order[x] = ifelse(my_order[x]==1, my_order[x], my_order[x-1]+1)
my_order})
Any idea how can I perform that with map? with something else? I am trying to avoid for i.