I have the following tibble x
.
I want to have the column new_col
populated with a number signifying what to divide the sum of columns 2017.2
, 2017.3
, 2017.4
, 2018.1
by. Columns 2 through 5 are quarterly visits to stores. A customer that has just started visiting in 2018 should not have their total transaction count divided by 4 quarters, as they are a new customer and have only come to us one quarter.
The following code in a for loop solves my question, but is taking too long to go through all the data (what appears to be multiple days).
x$new_col[i] <- 5 - min(which(x[i, 2:5] > 0))
How can I apply the 5 - min(which(x[i, 2:5] > 0))
to every row simultaneously as apposed to looping through each element? Open to suggestions within the tidyverse or in separate packages.