简体   繁体   中英

By batch, how can I run a for-loop for a R script?

Now I have 10 data sets for example named by out1sim.txt, out2sim.txt ... out10sim.txt .

My R script is a function that can load each data set and output out1.Rdata, out2.Rdata ... out10.Rdata .

Let's say the R script is test.R

How can I loop the function by batch? I mean when I do for-loop, how can I point the variable i into R script?

like in batch, I can do loop like this

for i in {1,10}
do # here I want to point this i into R script to make it load outisim.data and output outi.Rdata.

How can I do that? (by the way, my R code already has the function that load outisim.data and output outi.Rdata . I just want to know how to point i into R script.)

done

Create a vector of your input file names.

file_n<-c("out1sim.txt",...,"out10sim.txt")
for (i in 1:length(file_n)) {
    infile<-read.table(file_n[i],sep="\t")
    save(infile,file=paste0(out[i],".RData"))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM