Home > Mobile >  Looping through subdirectories, reshaping matrix and saving output in R
Looping through subdirectories, reshaping matrix and saving output in R

Time:10-11

I have a project folder (project1) which contains data from 20 Subjects (Sub-001, Sub-002 etc.). Each Subject's folder contains a subfolder (named AD) which has an AD.csv correlation matrix inside.

The folder structure looks like this

                                            ├── Sub-001
                                            │   └── AD
                                            │       └── AD.csv
                                            └── Sub-002
                                                └── *

I want to write a for loop that iteratively goes into each Subject's AD folder, reshapes the AD.csv matrix from wide to long, and saves the reshaped matrix as a new subj_AD_reshaped.csv file, named after the subject's ID (e.g., Sub-001_AD_reshaped.csv), in the Subject's folder.

The code I have written:

files<-list.files("group/S/project1/")
subjects<-list.dirs(path=files, full.names=T, recursive=F)
subj.AD<-subjects/"AD"

for (subj.AD in 1:length(subjects)) {
        df<-read.csv("matrix.csv", sep="", header=F)
        df.matrix<-as.matrix(df)
        df.matrix[lower.tri(df.matrix)]<-NA
        df.matrix.melt<-as.data.frame(reshape2::melt(df.matrix, na.rm=T))
        write.csv(df.matrix.melt, file="subjects/Subject_AD_reshaped.csv))

}

This code doesn't work mainly because R doesn't seem to recognise the AD folder for both input and output. What's the best way to write this for-loop?

CodePudding user response:

Maybe the following code will solve the problem.

base_path <- "group/S/project1"
subjects <- list.dirs(path = base_path, full.names = TRUE, recursive = FALSE)

for(s in subjects){
  fl <- file.path(s, "AD", "AD.csv")
  df1 <- read.table(fl, sep = ",")
  df1.matrix <- as.matrix(df1)
  is.na(df1.matrix) <- lower.tri(df1.matrix)
  df2 <- as.data.frame(df.matrix)
  df2.melt <- reshape2::melt(df2, na.rm = TRUE)
  out_file <- paste0(basename(s), "_AD_reshaped.csv")
  out_file <- file.path(s, out_file)
  write.table(df2.melt, out.file, sep = ",", row.names = FALSE)
}
  • Related