If it fits in storage you can do something like the code below.
If not you have to loop over the columns as in summarizing by group and linking the total group n with individual id . But then you have loop over the column numbers .
sales <- data.frame(
Itemnumber = c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"),
salesJan = c(0,1,1,1,1,2,0,0,0,0,0,1,2,1,2),
salesFeb = c(0,0,NA,0,1,2,1,1,0,0,1,1,1,2,1),
salesMar = c(0,2,1,1,2,1,2,0,0,0,0,0,2,1,2),
salesApr = c(0,1,1,1,1,1,NA,0,0,0,2,1,2,1,2),
salesMay = c(0,1,1,1,1,1,1,1,2,2,1,2,1,2,1),
salesJune = c(0,1,2,2,2,2,1,2,1,2,0,1,0,1,2),
salesJuly = c(0,2,1,2,1,1,0,0,1,2,0,1,2,0,1),
salesAug = c(0,0,0,0,0,0,1,2,1,0,0,1,2,1,NA),
salesSep = c(0,1,2,0,0,0,0,0,1,2,1,0,1,2,1),
salesOct = c(1,0,1,2,NA,1,2,1,0,1,2,0,1,2,0),
salesNov = c(0,1,2,1,1,1,0,0,1,2,0,NA,1,2,1),
salesDec = c(0,1,2,NA,1,2,0,0,0,0,1,2,1,0,0)
)
salesmat <-data.matrix(sales[,-1])
salesmat2 <- apply(salesmat,1:2,function(x) ifelse(is.na(x),0,x))
salesmat3 <- apply(salesmat2,1:2,function(x) pmin(1,x,na.rm=F))
salesmatc <- t(apply(salesmat3,1,function(x) cumsum(x)))
salesmatd <- cbind(matrix(0,nrow=nrow(salesmatc),ncol=4),salesmatc)
salesmats4 <- salesmatd[,5:ncol(salesmatd)] - salesmatd[,1:(ncol(salesmatd)-4)]
salesmats4
#> salesJan salesFeb salesMar salesApr salesMay salesJune salesJuly salesAug
#> [1,] 0 0 0 0 0 0 0 0
#> [2,] 1 1 2 3 3 4 4 3
#> [3,] 1 1 2 3 3 4 4 3
#> [4,] 1 1 2 3 3 4 4 3
#> [5,] 1 2 3 4 4 4 4 3
#> [6,] 1 2 3 4 4 4 4 3
#> [7,] 0 1 2 2 3 3 2 3
#> [8,] 0 1 1 1 2 2 2 3
#> [9,] 0 0 0 0 1 2 3 4
#> [10,] 0 0 0 0 1 2 3 3
#> [11,] 0 1 1 2 3 2 2 1
#> [12,] 1 2 2 3 3 3 4 4
#> [13,] 1 2 3 4 4 3 3 3
#> [14,] 1 2 3 4 4 4 3 3
#> [15,] 1 2 3 4 4 4 4 3
#> salesSep salesOct salesNov salesDec
#> [1,] 0 1 1 1
#> [2,] 3 2 2 3
#> [3,] 3 3 3 4
#> [4,] 2 2 2 2
#> [5,] 2 1 1 2
#> [6,] 2 2 2 3
#> [7,] 2 2 2 1
#> [8,] 2 2 2 1
#> [9,] 4 3 3 2
#> [10,] 3 3 3 3
#> [11,] 1 2 2 3
#> [12,] 3 2 1 1
#> [13,] 3 4 4 4
#> [14,] 3 3 4 3
#> [15,] 3 2 2 2
Created on 2021-09-05 by the reprex package (v2.0.0)