I'm basically taking a non normal dataset and converting it into a dataset that I can load into a SQL Server table. Using the example code below, is there a more efficient way to do this without having to explicity list the row indices of "ASRN1" or the dataframes I want spread, merge and bind? I have hundred of datasets i have to loop through, and some might have 3 sets of asrn1, service, and OCR, while others may have 30 sets of asrn1, service and ocr.
Columns<-c("SERIVCE ORDER", "SERVICE ORDER DATE", "ASRN1", "SERVICE","OCR","ASRN1","SERVICE","OCR", "ASRN1", "SERVICE", "OCR", "COMMENTS")
Values<-c("peanuts", "06/09/2020","1111", "abcd","xxxx", "2222", "efgh", "yyyy", "3333", "ijkl", "zzzz", "zippitydoda" )
df <- data.frame(Columns, Values)
a = which(df$Columns == "ASRN1",arr.ind=FALSE, useNames = TRUE)[1]
b = which(df$Columns == "ASRN1",arr.ind=FALSE, useNames = TRUE)[2]
c = which(df$Columns == "ASRN1",arr.ind=FALSE, useNames = TRUE)[3]
dfa<-spread(unique(df[0:(a-1),]),Columns,Values)
dfb<-spread(df[a:(b-1),],Columns, Values)
dfc<-spread(df[b:(c-1),],Columns,Values)
dfe<-spread(tail(df,-c+1),Columns,Values)
dff<-merge(dfa,dfb)
dfg<-merge(dfa,dfc)
dfh<-merge(dfa,dfe)
dfj<-dplyr::bind_rows(dff, dfg,dfh)