Here is the code in R: Make sure that all for the files (excel files with .xls or .xlsx) are in the same format as in link furnished above (2nd column is mz, 5 is RT and 11th column onward samples). Code would create one excel file per sample and name of the excel file would be sample_"sample name".xls. It will have three columns: mz, RT and sample name.
library(readxl)
library(WriteXLS)
df=data.frame(read_xls("example.xls"))
for (i in 11:ncol(df)){
temp_df=df[,c(2,5,i)]
WriteXLS(temp_df, ExcelFileName = paste0("sample_",names(df)[i],".xls"))
}
if you have multiple xls files in the folder (and only xls files of interest):
setwd("~/Desktop/test") # change this to directory of interest
fn=list.files(pattern = "\\.xls") # lists files with .xls extension
library(readxl)
library(WriteXLS)
for (i in fn){
df=data.frame(read_xls(i))
for (j in 11:ncol(df)){
temp_df=df[,c(2,5,j)]
WriteXLS(temp_df, ExcelFileName = paste0(sub('\\.xls$', '', i) ,"_","sample_",names(df)[j],".xls"))
}
}
output files will have "xlsfilename_samplename.xls"
By the time you found the solution you would have easily done this manually. Or is this a task which you have to do repeatedly?
The link with bioinformatics is not immediately obvious. Please elaborate.
Excel is not our 'favorite' tool but if you would convert the file to a text file you would have plenty of options.
Its a task i have to do it repeatedly on many different files. I have posted the example above.
I dont mind to convert the excel file into other types of file to get the job done, and then convert back to excel.
@OP: You don't have to convert excel into other formats if your excel files are manageable in size and consists of uniform format. Both R and Python are capable of handling (input and output) excel files.
Excel is most of the time not appropriate for data analysis. Only if you understand very well what you are doing...
For example it is not reproducible and doesn't track what you are doing. You cannot figure out what you did to a dataset months ago and you can silently change data without ever noticing. Keep your data in a normal file and use bash/R/Python/... and save your commands. That's reproducible and you can easily explain what happened and how you obtained a result.