I have a list of sales of different articles during time in the following format:
col <- c("A", "B", "C")
A <- c(1,0,0)
B <- c(0,1,0)
C <- c(0,0,1)
colnames(df) <- c('article','w1', 'w2', 'w3')
df
article w1 w2 w3
A 1 0 0
B 0 1 0
C 0 0 1
What I need is to create a new column that will calculate the mean of the rows, but starting only after first positive occurence in the row. That means that if a row looks like:
A 0 1 0
The algorithm has to take into account only last two values (1 and 0) and to place the value (1+0)/2 = 0.5 into the new column. The final result has to look like this:
article w1 w2 w3 Mean
A 1 0 0 0.33
B 0 1 0 0.5
C 0 0 1 1
Can, please, anyone tell me how to get it right?
Thanks a lot