Skip to content

Iteration through a list in R

1 message · MacQueen, Don

#
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