[R] list construction with automatic names

Alex van der Spek doorz at xs4all.nl
Thu Jul 4 14:04:47 CEST 2013

I often find myself (wanting t)o constructing lists or data.frames like so:

#Find files in subdirs
ii <- 0
for (ix in id) {
	ii <- ii + 1
	if (ii == 1) {
		fl <- list(basename(ix) = list.files(ix))
	} else {
		fl <- c(fl, list(basename(ix) = list.files(ix)))

The above is for list construction. It does not work as the argument name
is not accepted. In python this would be legal but I can't find how to
modify the name argument in R to make this legal.

Furthermore the use of a separate counter is not very elegant. Would there
be a better way? Again, in python appending to a list is very easy but
seems to be available in R only for vectors not lists?

Any help much appreciated!
Alex van der Spek

More information about the R-help mailing list