mahmood
mahmood

Reputation: 1213

How to load several RData with a loop?

I want to load several RData files into R. The code I use is

for(i in 1:100){
    name_i <- paste('path/file_',i,'.RData', sep="")
    load(name_i)
}

and I have also tried:

for(i in 1:100){
    paste('name_',i,sep='') <- paste('path/file_',i,'.RData', sep="")
    load(name_i)
 }

which resulted in this error:

object 'name.in' not found

What I want is to have each RData to be loaded as:

name_1
name_2
.
.
.
name_100

but this is obviously not working. Can anyone give me a solution.

bests and thanks in advance

Upvotes: 2

Views: 3868

Answers (1)

csgillespie
csgillespie

Reputation: 60522

Your paste line is wrong. This

paste('name_',i,sep='') <- paste('path/file_',i,'.RData', sep="")

should be something like ('m note sure of your exact file name).

fname = paste('path/file_',i,'.RData', sep="")
load(fname)

It's also worth thing about using list.files, so

list.files("path/", pattern="*.RData", full.names="TRUE")

Then looping through the file names.

Upvotes: 2

Related Questions