[R] Iteration through a list in R

David Winsemius dwinsemius at comcast.net
Thu Feb 28 23:10:59 CET 2013

On Feb 28, 2013, at 4:30 AM, Sahana Srinivasan wrote:

> Hello :)
> I'm just starting out with R and would appreciate your help with a couple
> of problems I am running into.

You do need to work through the examples in "Introduction to R"

> I have used Sys.glob to get a list of all filenames having a particular
> file extension (in my case, *.txt)

When posting to rhelp you should offer code as well as a description.

> I would now like to use this list in the following manner: I would like to
> use each filename from the list and open that file into a tab separated
> matrix and proceed.


"Matrix" is sometimes used as a generic English word describing a regular arrangement of data. When discussing the R language, you should use it only when talking about a specific sort of data-object which will return TRUE for: is.matrix(object). And then depending on whether you actually want a matrix or will be satisfied by the data-frame object that read.delim would have returned, you may or may not need:


> How can I go about iterating through each filename in the list and then
> opening each of the files?

lapply( fil_list, read.delim)

> I believe as.matrix can be used to open the txt file as a table, is that
> correct?

No. Please read the help page for `as.matrix` more carefully. It does not take a file or connection argument.

> I know these are beginner queries but I would love your help. Thank you in
> advance :)


David Winsemius
Alameda, CA, USA

More information about the R-help mailing list