Run R script on 100 files using for loop
1
1
Entering edit mode
4.4 years ago
Hann ▴ 110

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.4k views
ADD COMMENT
0
Entering edit mode

fread(fileNames) instead of fread(file)

ADD REPLY
1
Entering edit mode
4.4 years ago

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

ADD COMMENT
0
Entering edit mode

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 REPLY
1
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2495 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6