How can each file be commanded separately with multiple files in R??
Hi I am new to R.
I have multiple files and I want to command these files separately. (I have about 200 files)
Using for loop or lapply, how can I command these for each file automatically? I want to tag sample names in Barcode and filenames like sample1, sample2, sample3...
sample1 <- read.delim("sample1.hg38_multianno.txt", header = F)
colnames(sample1)[1:10] = c("Chr", "Start", "End", "Ref", "Alt", "Func.refGene", "Gene.refGene", "GeneDetail.refGene", "ExonicFunc.refGene","AAChange.refGene")
sample1 <- sample1[30:nrow(sample1),]
sample1 <- sample1[,-c(11:87)]
sample1$Tumor_Sample_Barcode = "sample1"
write.table(sample1, file="sample1.txt", row.names = FALSE, sep = "\t", quote = FALSE)
sample1.maf = maftools::annovarToMaf(annovar = "sample1.txt",
Center = NULL,
refBuild = 'hg38',
tsbCol = 'Tumor_Sample_Barcode',
table = 'refGene')
write.table(sample1.maf, file="sample1.maf", row.names = FALSE, sep = "\t", quote = FALSE)
Thank you in advance
• 1,391 views
•
link
2 answers
I'm sure there is a better way to do this, but you could declare a variable outside the loop and increment it inside the loop (or use it in lapply) like so:
lapply(1:200, function(i) {
sample_name <- paste0("sample", i)
sample_file <- read.delim(paste0(sample_name,".hg38_multianno.txt", header = F)
colnames(sample_file)[1:10] = c("Chr", "Start", "End", "Ref", "Alt", "Func.refGene", "Gene.refGene", "GeneDetail.refGene", "ExonicFunc.refGene","AAChange.refGene")
sample_file <- sample_file[30:nrow(sample_file),]
sample_file <- sample_file[,-c(11:87)]
sample_file$Tumor_Sample_Barcode = sample_name
write.table(sample_file, file=paste0(sample_name, ".txt"), row.names = FALSE, sep = "\t", quote = FALSE)
sample_file.maf = maftools::annovarToMaf(annovar = paste0(sample_name, ".txt"),
Center = NULL,
refBuild = 'hg38',
tsbCol = 'Tumor_Sample_Barcode',
table = 'refGene')
write.table(sample_file.maf, file=paste0(sample_name, ".maf"), row.names = FALSE, sep = "\t", quote = FALSE)
})
• 0 views
•
link
While it is recommended to use apply function instead of for loop in R (like what Ram did), I understand the fucntion behavior using for loop much more better!! Here is a for loop solution :
files = list.files() # assuming you have only the intended files in the current working directory
n = #column number of the "Tumor_Sample_Barcode" in your files
for (f in files){
tmpDf<- read.delim(files[f], header = F)
tmpDf <- tmpDf[30:nrow(tmpDf),]
tmpDf <- tmpDf[,-c(11:87)]
tmdDF[, n] = substr(files[f],1,7)
write.table(tmpDf, file=paste0(substr(files[f],1,7), "txt"), row.names = FALSE, sep = "\t", quote = FALSE)
tmp.maf = maftools::annovarToMaf(annovar = paste0(substr(files[f],1,7), "txt"),
Center = NULL,
refBuild = 'hg38',
tsbCol = 'Tumor_Sample_Barcode',
table = 'refGene')
write.table(tmp.maf, file=paste0(substr(files[f],1,7), "maf"), row.names = FALSE, sep = "\t", quote = FALSE)
}
• 0 views
•
link
Log in to answer this question.