Re: [R] Iterating Named List

From: jim holtman <jholtman_at_gmail.com>
Date: Tue, 05 Aug 2008 10:39:06 -0400

'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?

______________________________________________
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.
Received on Tue 05 Aug 2008 - 14:46:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Aug 2008 - 16:33:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive