Question: Run R script on 100 files using for loop
1
gravatar for Hann
14 months ago by
Hann80
KAUST
Hann80 wrote:

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 • 282 views
ADD COMMENTlink written 14 months ago by Hann80

fread(fileNames) instead of fread(file)

ADD REPLYlink written 14 months ago by German.M.Demidov1.9k
1
gravatar for Nicolas Rosewick
14 months ago by
Belgium, Brussels
Nicolas Rosewick9.3k wrote:

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

ADD COMMENTlink modified 14 months ago • written 14 months ago by Nicolas Rosewick9.3k

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

ADD REPLYlink written 14 months ago by Hann80
1

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

ADD REPLYlink written 14 months ago by Nicolas Rosewick9.3k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1487 users visited in the last hour