Trying to read multiple CSV files in a folder and add filename to each column in R

1

Hello Everyone,

I have multiple csv files (27 files of extracted features of MRI patients). Each csv file has different number of rows but the same set of columns. I am going to read each file and try to add file name to each columns' names. For instance, for file 27 with "AX_FLAIR-parcellationROI_27" name, I want to add "AX_FLAIR-parcellationROI_27" to each columns' names.
To work on all the files at the same time , I put all files in a list and use a loop to read each file. I am using "paste" function in order to add new string to each column, but I got an error.

files <- list.files(pattern = "/Users/mostafa/Documents/Mostafa/UM/Papers/Data/TCIA/TCGA-GBM/RadiomicsData_EC/TCGA-02-0009/RadiomicsFeatures-csv/AX_FLAIR")

for(f in files){ 
  ReadInFile <- read.csv(file=f, header=T, na.strings="NULL")
  colnames(ReadIFile[f])=paste(colnames(ReadInFile[f]),"_AX_FLAIR-parcellationROI_",f)
}

Is there a way to fix this last piece of code? Thanks.


R


Merging

• 65 views



Source link