This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Run R script on 100 files using for loop

Hello,

I need to run this small R script from LEA r package for 100 files:

mb10 <- fread("wind43.geno") # this is the file after replacing -1 by 9
write.geno(mb10, "genotypes_chr09A_10mb.geno")
output = geno2lfmm("wind43.geno")

I think one option is to do for loop, I have written this:

the_dir <- "pca_1mbp_window/GENO/geno"
fileNames <- Sys.glob("*.geno")
for (file in fileNames) {
# read data:
wind <- fread(fileNames)
write.geno(wind, the_dir, basename(file))
output=geno2lfmm("file")
}

But i am getting this error:

Error in fread(fileNames) : input= must be a single character string containing a file name, a system command containing at least one space, a URL starting 'http[s]://', 'ftp[s]://' or 'file://', or, the input data itself containing at least one \n or \r

Is this the correct way? How I would run the script for 100 files at once?

r

1 answer

change wind <- fread(fileNames) to wind <- fread(file) and output=geno2lfmm("file") to output=geno2lfmm(file)

Thank you! but I am getting this error: Error in write.geno(wind, the_dir, basename(file)) : unused argument (basename(file))

from the function write.geno

try write.geno(wind, paste0(the_dir, basename(file)))

Log in to answer this question.