[R] Iterating Named List

jim holtman jholtman at gmail.com
Tue Aug 5 16:39:06 CEST 2008


'lapply' is your friend:

lapply(y, function(.element) {...process each element of the list....})

> y <- list(a=1:10, b=letters, c=LETTERS)
> invisible(lapply(y, function(.ele) {print(.ele); cat("------\n")}))
 [1]  1  2  3  4  5  6  7  8  9 10
------
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p"
"q" "r" "s" "t" "u" "v" "w" "x"
[25] "y" "z"
------
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P"
"Q" "R" "S" "T" "U" "V" "W" "X"
[25] "Y" "Z"
------
>


On Tue, Aug 5, 2008 at 10:16 AM, Gundala Viswanath <gundalav at gmail.com> wrote:
> Hi all,
>
> I have the following named list:
>
>> print(y)
> $`200052_s_at`
> [1] -1066.975 -1063.893 -1062.815 -1062.121 -1059.004
>
> $`200071_at`
> [1] -959.823 -953.980 -953.886 -948.781 -974.890
>
> $`200084_at`
> [1] -1135.804 -1132.863 -1128.197 -1128.633 -1125.890
>
>
> What I want to do is to iterate this name list and process its members.
> To do that I attempt the following code (but failed):
>
> __BEGIN__
> ny <- names(y)
>
> for (i in ny) {
>   val <- paste("`",i,"`",sep="")
>   print(y$val)
>
>   # later we want to process y$val
> }
>
> __END__
> However the after printing it gives "NULL".
> What's wrong with my code above?
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list