[R] naming list entries dynamically

Torsten Lange tlange at gwdg.de
Fri Oct 24 12:19:25 CEST 2008


Hello,

Thank you for all answers! The function works!

Torsten


fload.from.folder <- function(pattern) {

  fname <- list.files(pattern=pattern)
  flist <- sapply(fname, read.table, header=FALSE)

  for(i in seq_along(fname)) {
    flist[[i]] <- t(read.table(fname[i], header=FALSE))
    }

  invisible(flist)
}

>On Thu, Oct 23, 2008 at 10:35 PM, Gábor Csárdi <csardi.gabor at gmail.com> wrote:
>  
>
>>On Thu, Oct 23, 2008 at 10:29 PM,  <tlange at gwdg.de> wrote:
>>    
>>
>>>Hello,
>>>
>>>I'm new to R and I'd like to dynamically assign names to the entries of a
>>>list.
>>>The situation is: I load several ascii-files from a folder. The data shall
>>>be stored within the list structure, where the file names are assigned to
>>>the list entries.
>>>
>>>      
>>>
>>>>flist
>>>>        
>>>>
>>>$file1.txt
>>> [1]  1.0  1.2  1.4  1.6  1.8  2.0...
>>>$file2.txt
>>> [1]  1.0  1.2  1.4  1.6  1.8  2.0...
>>>
>>>That's what I would like to see. But I got stucked coding it. Web search
>>>doesn't helped me a lot. Here is some code I thought it somehow would work
>>>like this:
>>>
>>><----
>>>flist <- list()
>>>fname <- list.files(pattern="some_pattern.txt")
>>>for(i in 1:length(fname)) {
>>>  flist$"how_do_I_name_it_dynamically?" <- read.table(fname[i],
>>>header=FALSE)
>>>      
>>>
>>Do
>>
>>flist[[ fname[i] ]] <- ....
>>
>>Btw. 1:length(fname) will fail as soon as fname will be empty. Do
>>seq_along(fname) instead.
>>    
>>
>
>Btw2. is is probably simpler to write
>
>fname <- list.files(pattern="some_pattern.txt")
>flist <- sapply(fname, read.table, header=FALSE)
>
>Gabor
>  
>



More information about the R-help mailing list