Hi,
I don't know the read_file
command, but if the input file is also an Excel file I'd do this
library(tidyverse)
library(psy)
library(xlsx)
#Number of rows before to take into account
rolling = 2
#Load the data
sales1 = read.xlsx("sales1.xls", 1)
#Lag
sales1 = sales1 %>% mutate(lagsales = lag(sales))
#Get the rolling alpha
sales1$alpha = c(
rep(NA, rolling),
map_dbl((rolling + 1):nrow(sales1), function(x){
cronbach(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$alpha
})
)
write.csv(sales1, "sales1.csv", row.names = F)
#OR
write.xlsx(sales1, "sales1.xlsx", row.names = F)
PJ