#install.packages("dplyr")
#install.packages("writexl")

library("readxl")
library("dplyr")
library("openxlsx")

path = "C:/Users/koenv/OneDrive - WageningenUR/PHD onedrive/4. Papers/3 effluents fractionation/data/Significants/"
setwd(path)


sns = excel_sheets("MP data.xlsx")
groups = list('Group 1', 'Group 2', 'Group 3')
pv = list()
fractions = unique(d$Fraction)[-1]
OUT <- createWorkbook()

for (sn in sns){
  d = as.data.frame(read_excel("MP data.xlsx", sheet = sn)) 
  dfp = data.frame(fractions)
  
  for (g in groups) {
    dg = select(d,starts_with(g))
    
    for (i in 0:5){
      l1 = 2+i*5
      h1 = 6+i*5
      d1 = data.matrix(dg[l1:h1,])
      
      for (j in 0:5){
        l2 = 2+j*5
        h2 = 6+j*5.
        d2 = data.matrix(dg[l2:h2,])  
        p = wilcox.test(d1,d2)[3]
        pv = append(pv,p)
        print(paste(sn,g,i,j))
      }
      cn = paste(fractions[i+1], substr(g,6,8))
      dfp[cn] <- as.numeric(unlist(pv))
      pv <- list ()
      
  }}
  addWorksheet(OUT, sn)
  writeData(OUT, sheet = sn, x = dfp) 
}
saveWorkbook(OUT, "p data.xlsx", overwrite=TRUE)

sn
