Question: Run R script on 100 files using for loop
1
gravatar for haneenih7
21 days ago by
haneenih750
haneenih750 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 • 102 views
ADD COMMENTlink written 21 days ago by haneenih750

fread(fileNames) instead of fread(file)

ADD REPLYlink written 21 days ago by German.M.Demidov850
1
gravatar for Nicolas Rosewick
21 days ago by
Belgium, Brussels
Nicolas Rosewick8.5k wrote:

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

ADD COMMENTlink modified 21 days ago • written 21 days ago by Nicolas Rosewick8.5k

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 21 days ago by haneenih750
1

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

ADD REPLYlink written 21 days ago by Nicolas Rosewick8.5k
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: 1892 users visited in the last hour