unzipping, merging, and adding a header column

I have a zipped (.gz) dataset which contains about 1000 folder. Each folder contains 7 days raw accelerometer data with column X, Y, Z. I need help to unzip all the folder, merged all files together, add a custom header, and rename the folder. Lastly, I want to repeat this step in the same order for all folders.

Thank you.

Hi @joseg ,

here is a code example that should get you started

# zipped (.gz) dataset 
pathGZ <- "<your path >somefoldername.tar.gz"
pathToUnpackTo <- "<your path to unpack to>"
#  I need help to unzip all the folder
unpack <- untar(tarfile = pathGZ,
                exdir = pathToUnpackTo)  
path <- file.path(pathToUnpackTo, "somefoldername")
dirs <- list.dirs(path = path,
                  full.names = TRUE, 
                  recursive = TRUE)
#add a custom header
customHeader <- c("MyHeader1", "Myheader2", "Myheader3")

for (dir in dirs) {
  if(dir == path) next 
  files <- list.files(dir,
                      full.names = TRUE)
  combinedFile <- df <- data.frame(MyHeader1=character(),
                                   MyHeader2=character(), 
                                   MyHeader3=character(), 
                                   stringsAsFactors=FALSE) 
 #  merged all files together
  for (file in files) {
    temp <- read.table(file, header = TRUE, sep = "\t")
    names(temp) <- customHeader
    combinedFile <- rbind(combinedFile, temp)
  }
  write.table(x = combinedFile, 
              file = file.path(dir, "combinedFile.txt"),
              sep = "\t", 
              row.names = FALSE)
  # here you could rename your folder but that's up to you to decide of what the renaming should be  
}