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.
There is easier way (works in zsh and bash)
new files will have "new.csv" extension. It helps if you could post data. Hopefully, text within quotes, do not have comma inside them.
Thanks for you guidance. Is your command in R? If not, how can I do it in R?
Try this in R:
This would add file name to each column and all new files are stored in R as objects for further manipulation.
ls()
would list the objects and objects with "_new" are the objects with filename appended in all columns. change the path in code, as per your data location on local machineGreat!! I tried it, but I got the following error:
How can I deal with that?
PS: I am using a mac to run the code. Maybe there is a problem with this command:
Please post the code you have used and also tree of the directory, from where the code is used. print
getwd()
in R to get active directory.Thanks, the issue is solved!!
it would be helpful for others if you could post how the issue is resolved.
Here is how the issue is resolved: