Dear all,
I would like to do read a bunch of files that have the following format
file_i_j
examples:
file_1_1
file_1_2
file_1_3
file_2_1
file_2_2
file_3_1
file_3_2
file_3_3
file_3_4
file_4_1
file_4_2
The integer i goes from 1 to 100
and the integer j starts from 1 and stops somewhere between 1 and 100, which I
do not know in advance
Usually I would be able to solve this by having a nested loop
(algorithmic syntax below, not sure if is R okay)
for i in (c(1:100))
for j in (c(1:100))
store[i,j]<- read file_i_j
end
end
the problem with that is that R will halt when j take a value that refers to a
non existing value. Thus I want to have a good way to break the nested loop and
continue with the outer one. How I can do that?
Best Regards
Alex
On 11-05-24 6:00 AM, Alaios wrote:> Dear all, > I would like to do read a bunch of files that have the following format > > file_i_j > examples: > file_1_1 > file_1_2 > file_1_3 > file_2_1 > file_2_2 > file_3_1 > file_3_2 > file_3_3 > file_3_4 > file_4_1 > file_4_2 > > The integer i goes from 1 to 100 > and the integer j starts from 1 and stops somewhere between 1 and 100, which I do not know in advance > > Usually I would be able to solve this by having a nested loop > > (algorithmic syntax below, not sure if is R okay) > for i in (c(1:100)) > for j in (c(1:100)) > store[i,j]<- read file_i_j > end > end > > the problem with that is that R will halt when j take a value that refers to a non existing value. Thus I want to have a good way to break the nested loop and continue with the outer one. How I can do that?Use a while() loop, or use break. You can test for the existence of a file using the file.exists() function. Duncan Murdoch
You might use dir() to get the file names (if they are in the same folder), or something like dir(pattern="^file.*") if you want to read only some files from there. Then for storing the result as something like store[i,j] as in your example, you could split the file name using something like strsplit(filename, "_") and use the resulting components for i and j. Best regards, Kenn On Tue, May 24, 2011 at 1:00 PM, Alaios <alaios at yahoo.com> wrote:> Dear all, > I would like to do read a bunch of files that have the following format > > file_i_j > examples: > file_1_1 > file_1_2 > file_1_3 > file_2_1 > file_2_2 > file_3_1 > file_3_2 > file_3_3 > file_3_4 > file_4_1 > file_4_2 > > The integer i goes from 1 to 100 > and the integer j starts from 1 and stops somewhere between 1 and 100, which I do not know in advance > > Usually I would be able to solve this by having a nested loop > > (algorithmic syntax below, not sure if is R okay) > for i in (c(1:100)) > ?for j in (c(1:100)) > ? ?store[i,j]<- read file_i_j > ? end > end > > the problem with that is that R will halt when j take a value that refers to a non existing value. Thus I want to have a good way to break the nested loop and continue with the outer one. How I can do that? > > Best Regards > Alex > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >