What this script does

This script isolates 2-hour bins from the pre-prepared .csv files containing the first night of sleep data.

Three files for each Clinstag/COPS case will be created:
1. bin1.csv - the first two hours of data
2. bin2.csv - the second two hours of data
3. bin3.csv - the first 4 hours of data (combined bins 1 and 2)

An additional three files will be created with the WASO time (in minutes) and the sleep efficiency calculation (%) for each bin:

  1. Bin1_Results.csv
  2. Bin2_Results.csv
  3. Bin3_Results.csv

Before running this….
Make sure you have your RStudio working directory set to the location of your .csv files. The script will process all files in the directory

If the script has successfully completed it’s job, a conformation message is printed in the console.

# Load relevant packages
ipak <- function(pkg){
        new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
        if (length(new.pkg)) 
                install.packages(new.pkg, dependencies = TRUE)
        sapply(pkg, require, character.only = TRUE)
}
packages <- c("cosinor2", "tidyverse", "data.table", "stringr", "NISTunits")
ipak(packages)

Tools for file renaming if needed:

# Set your working directory to the folders with the Control group data
Con = list.files(pattern="Clinstag")
# Rename the files with the group extension
sapply(Con, FUN = function(eachPath) {
        file.rename(from = eachPath, to = sub(pattern = "Clinstag", replacement = "Con_Clinstag", eachPath))
})

# Set your working directory to the folders with the Control group data
MCI = list.files(pattern="Clinstag")
# Rename the files with the group extension
sapply(MCI, FUN = function(eachPath) {
        file.rename(from = eachPath, to = sub(pattern = "Clinstag", replacement = "MCI_Clinstag", eachPath))
})
# Assign all spreadsheets in wd() 
files <- Sys.glob("*.csv*")
# Returns the files in the directory  
files

# Isolate the different bins and save to .csv
batch_isolate <- lapply(files, function(f) {
  
  library(tidyverse)
  
  data <- read.csv(f, header = T, sep = ",")
  
  data$Epoch <- rep(1:nrow(data)) 
  
  bin1 <- slice(data[1:240, ])
  bin2 <- slice(data[241:480, ])
  bin3 <- slice(data[1:480, ])

  write.csv(bin1, paste0(f, "_Bin1.csv"))
  write.csv(bin2, paste0(f, "_Bin2.csv"))
  write.csv(bin3, paste0(f, "_Bin3.csv"))
  
  print(paste0(f, " 's file has been successfully isolated into two 2-hour bins in your working directory"))
  
})

# Assign all bin1 spreadsheets in wd() 
bin_1 <- Sys.glob("*Bin1*")
# Returns the files in the directory  
bin_1

for (filename in bin_1) {
        # Read data:
        data <- read.csv(filename, header = T, sep = ",")
        
        sleep_epochs <- data$Sleep.Wake
        count_sleep <- count(data$Sleep.Wake == 0)
        efficiency <- count_sleep/240*100
        WASO_seconds <- count(data$Sleep.Wake == 1)*30
        WASO_minutes <- WASO_seconds/60
        results <- data.frame(efficiency, WASO_minutes)
        
        # Write new data to a new excel file:
        write.table(results, "Bin1_Results.csv", 
                    append = T, sep = ",", row.names = paste(filename), col.names = T)
        
}

# Assign all bin2 spreadsheets in wd() 
bin_2 <- Sys.glob("*Bin2*")
# Returns the files in the directory  
bin_2

for (filename in bin_2) {
        # Read data:
        data <- read.csv(filename, header = T, sep = ",")
        
        sleep_epochs <- data$Sleep.Wake
        count_sleep <- count(data$Sleep.Wake == 0)
        efficiency <- count_sleep/240*100
        WASO_seconds <- count(data$Sleep.Wake == 1)*30
        WASO_minutes <- WASO_seconds/60
        results <- data.frame(efficiency, WASO_minutes)
        
        # Write new data to a new excel file:
        write.table(results, "Bin2_Results.csv", 
                    append = T, sep = ",", row.names = paste(filename), col.names = T)
        
}

# Assign all bin2 spreadsheets in wd() 
bin_3 <- Sys.glob("*Bin3*")
# Returns the files in the directory  
bin_2

for (filename in bin_3) {
        # Read data:
        data <- read.csv(filename, header = T, sep = ",")
        
        sleep_epochs <- data$Sleep.Wake
        count_sleep <- count(data$Sleep.Wake == 0)
        efficiency <- count_sleep/480*100
        WASO_seconds <- count(data$Sleep.Wake == 1)*30
        WASO_minutes <- WASO_seconds/60
        results <- data.frame(efficiency, WASO_minutes)
        
        # Write new data to a new excel file:
        write.table(results, "Bin3_Results.csv", 
                    append = T, sep = ",", row.names = paste(filename), col.names = T)
        
}