Skip to content
Prev 318551 / 398503 Next

Iteration through a list in R

I suppose you have your filenames stored in a character vector, which I
will name "myfiles". (Within R, it is not a "list"; lists have a special
structure).

There is no such thing as a "tab separated matrix" in R. "tab separated"
would refer to the file, I presume.

for (nm in myfiles) {

  tmpdat <- read.table(nm, ## put appropriate arguments here )

  tmpmat <- as.matrix(tmpdat)
   ## might or might give you what you want,
   ## depending on what is in the files

  ## it might make sense to use scan() instead, depending on how
  ## your text files are structured.

  ## do whatever with tmpdat or tmpmat

}

When the loop is done, however, tmpdat and tmpmat will have only the
values from the last file. Saving all of them for later use is a different
question.

Often, you will get better help from r-help if you show what you have
tried, by which I mean showing some R code. As much as possible, using
examples that other people can run for themselves. (See the posting guide)

Hope this helps

-Don
Message-ID: <5E1B812FAC2C4A49B3D99593B5A521912F35BD@PRDEXMBX-08.the-lab.llnl.gov>
In-Reply-To: <CACvtrYjDAdPyeBUvveNjsD0d49BWdu2pnvfZam_zeRed-LfDoA@mail.gmail.com>