I have what may be a simple/foolish question, but I've done the due diligence and looked through pages of posts here as well as several of the PDFs on the CRAN site, but haven't been able find what I'm after. I am working with a list of say 3 histogram objects A, B & C, and each histogram is a list of 7 elements. I would like to access $name, the 6th element, of histograms A,B and C. Trial and error yielded some results that told me I clearly don't understand how R interprets index commands. For the histogram list above: a[1:2] give histograms A and B as expected. a[[1:2]] gives the second element of histogram 1, but a[[1:1]] gives all elements of histogram 1, while a[[1:3]] gives null?! If anyone could help with an explanation of indexing rules, or a source that does so, I would very much appreciate it. Oh and an answer to the first question! Thanks All Jason -- View this message in context: http://n4.nabble.com/list-index-rules-evaluation-behavior-tp1745398p1745398.html Sent from the R help mailing list archive at Nabble.com.
Hi Jason, try using comma's instead of colons. eg a[[c(1,6)]], a[[c(3,6)]] etc... If you use a[[1:3]] this is equivalent to a[[c(1,2,3)]]. As the list only contains 2 levels, this will give an error or NULL , depending on your R version. More info you find by ?"[[" Cheers Joris On Tue, Mar 30, 2010 at 5:47 PM, Dgnn <sharkbrainpdx@gmail.com> wrote:> > I have what may be a simple/foolish question, but I've done the due > diligence > and looked through pages of posts here as well as several of the PDFs on > the > CRAN site, but haven't been able find what I'm after. > > I am working with a list of say 3 histogram objects A, B & C, and each > histogram is a list of 7 elements. I would like to access $name, the 6th > element, of histograms A,B and C. > > > Trial and error yielded some results that told me I clearly don't > understand > how R interprets index commands. For the histogram list above: > a[1:2] give histograms A and B as expected. > a[[1:2]] gives the second element of histogram 1, but a[[1:1]] gives all > elements of histogram 1, while a[[1:3]] gives null?! > > If anyone could help with an explanation of indexing rules, or a source > that > does so, I would very much appreciate it. Oh and an answer to the first > question! > > Thanks All > > Jason > > > -- > View this message in context: > http://n4.nabble.com/list-index-rules-evaluation-behavior-tp1745398p1745398.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
On Mar 30, 2010, at 11:47 AM, Dgnn wrote:> > I have what may be a simple/foolish question, but I've done the due > diligence > and looked through pages of posts here as well as several of the > PDFs on the > CRAN site, but haven't been able find what I'm after. > > I am working with a list of say 3 histogram objects A, B & C, and each > histogram is a list of 7 elements. I would like to access $name, the > 6th > element, of histograms A,B and C. >If you want better answers, you should provide better examples ... with _CODE_.> > Trial and error yielded some results that told me I clearly don't > understand > how R interprets index commands. For the histogram list above: > a[1:2] give histograms A and B as expected. > a[[1:2]] gives the second element of histogram 1, but a[[1:1]] gives > all > elements of histogram 1, while a[[1:3]] gives null?! > > If anyone could help with an explanation of indexing rules, or a > source that > does so, I would very much appreciate it. Oh and an answer to the > first > question!?"[[" "[[" always returns a single vector or list and so its arguments will be coerced to a single value. When passed an arguemnt that has multiple values it is interpreted as serial application of "[[" with the serial values. The construction [[1:1]] gets turned into [[1]] (since 1:1 is just 1) while the construction [[1:2]] got turned into [[1]][[2]] > list(a=list(aa=5, bb=6),b=2,c=3)[[1:2]] [1] 6 "[" may return a more complex object and so may accept multiple arguments > list(a=1,b=2,c=3)[c(1,3)] $a [1] 1 $c [1] 3>-- David Winsemius, MD West Hartford, CT
Sorry for not supplying some example code for the above example. Here's an example list 'a' with histogram elements A, B, and C which are also lists.>a$A $breaks [1] -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 $counts [1] 1 0 0 2 29 120 301 433 421 265 93 43 9 3 $intensities [1] 5.81e-05 0.000000e+00 0.000000e+00 1.162791e-04 1.686047e-03 6.976744e-03 1.750000e-02 [8] 2.512e-02 2.447674e-02 1.540698e-02 5.406977e-03 2.500000e-03 5.232558e-04 1.744186e-04 $density [1] 5.813-05 0.000000e+00 0.000000e+00 1.162791e-04 1.686047e-03 6.976744e-03 1.750000e-02 [8] 2.517442e-02 2.447674e-02 1.540698e-02 5.406977e-03 2.500000e-03 5.232558e-04 1.744186e-04 $mids [1] -75 -65 -55 -45 -35 -25 -15 -5 5 15 25 35 45 55 $xname [1] "X[[1L]]" $equidist [1] TRUE attr(,"class") [1] "histogram" $B $breaks [1] -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 $counts [1] 2 1 3 20 86 225 396 408 404 294 182 125 38 11 1 ... $C ... Is there a way to index the $xname element from each of the elements of a? -- View this message in context: http://n4.nabble.com/list-index-rules-evaluation-behavior-tp1745398p1745579.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- Extract vector elements until cumsum <= x
- lapply and list indexing basics
- Writing a .pdf file within a function - what do I need to return()?
- lapply and list indexing basics (after realizing I wasn't previously subscribed...sorry)
- partial duplicates of dataframe rows, indexing and removal