create a loop: convert .txt to .csv in R -
i try convert .txt files in .csv, didn't manage create loop. actual line 1 file (which works perfectly) following:
tab = read.delim("name_file", header = true, skip = 11) write.table(tab, file="name_file.csv",sep=",",col.names=true,row.names=false)
and .txt file have in wd.
i tried loop with, based on reasearch on web, not sure it's right one:
files = list.files(pattern = ".txt") (i in 1:length(files)) { files = read.csv(file = files[i], header = true, skip = 11, fill = true) write.csv(files, file = paste0(sub("folder_name", ".txt","", files[i]), ".csv")) }
i'm on windows system. appreciate help... thanks!
you need index output inside loop well. try this:
infiles = list.files(pattern = ".txt") outfiles = vector(mode = "character", length = length(infiles)) (i in 1:length(infiles)) { outfiles[i] = read.csv(file = infiles[i], header = true, skip = 11, fill = true) write.csv(outfiles[i], file = paste0("folder_name", sub(".txt","", infiles[i]), ".csv")) }
Comments
Post a Comment