For some reason I did not receive your email.
Sorry for the inconvenience caused





________________________________
From: Dennis Murphy <djmu...@gmail.com>

Cc: Rhelp <r-help@r-project.org>
Sent: Wed, September 22, 2010 1:46:28 PM
Subject: Re: [R] efficient list indexing

Hi:

I believe we had this discussion yesterday, 
http://r.789695.n4.nabble.com/Object-oriented-programming-in-R-td2538541.html#a2538916


but since you chose to repeat that message, it clearly wasn't enough, so start 
with 


http://cran.r-project.org/doc/manuals/R-intro.html#Lists
http://stackoverflow.com/questions/2050790/how-to-correctly-use-lists-in-r

With the following example as a reference:
Empl <- list(employee = "Anna", spouse = "Fred", children = 3,
            child.ages = c(4, 7, 9))

we quote Venables and Ripley (2002, p. 45):
  "It is important to appreciate the difference between [ and [[. The [ form 
extracts sub-vectors, so Empl[2] is a list of length one, whereas Empl[[2]] is 
a 
component (a character vector of length one)."

In that context, consider the following:
> Empl$employee
[1] "Anna"
> Empl$child.ages[2]
[1] 7
> x <- "spouse"; Empl[[x]]
[1] "Fred"
> Empl[[3]]
[1] 3
> Empl[[4]]
[1] 4 7 9
> Empl[[4]][2]
[1] 7
> Empl[4]
$child.ages
[1] 4 7 9
> Empl[4][2]   # Why?
$<NA>
NULL
> Empl[2]
$spouse
[1] "Fred"
> Empl[[2]]   # Why the difference?
[1] "Fred"


A somewhat more expansive discussion is found on pp. 12-13 of Venables and 
Ripley (2000), S Programming.

Dennis




Hello everyone,
>
>I need some help with lists inside lists (a good way to emulate a struct)\
>
>Assume that there is a small list called fred:
>fred <- list(happy = 1:10, name = "squash")
>
>
>and a big list called bigfred that includes fred list 5 times
>bigfred <- rep(fred,5)
>
>Is it possible somehow to index all these sublists(fred) inside bigfred with a
>more direct way like this:
>bigfred[1] shows the first sublist fred
>bigfred[2][2] shows the second sublist fred, the second element of the fred 
list
>
>So far I found some way to do this by refering to the sublists by the 
following:
>bigfred[1+index*length(fred)]
>where index shows the beginning of a sublist.
>
>I would like to thank you in advance for your help
>Best Regards
>Alex
>
>
>
>
>       [[alternative HTML version deleted]]
>
>______________________________________________
>R-help@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.
>



      
        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to