[R] naming list entries dynamically
Gábor Csárdi
csardi.gabor at gmail.com
Thu Oct 23 22:38:37 CEST 2008
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
>> }
>> ---->
>>
>> Second: I assume to use lists in R more or less like Matlab-structs. I
>> hope that's not compleatly wrong... ?
>
> That I don't know.
>
> Gabor
>
>> Appreciating any help and hints,
>> Torsten
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Gabor Csardi <Gabor.Csardi at unil.ch> UNIL DGM
>
--
Gabor Csardi <Gabor.Csardi at unil.ch> UNIL DGM
More information about the R-help
mailing list