s-news
[Top] [All Lists]

Re: indexing within a list using Splus 2000 and Splus 6

To: s.su@qut.edu.au
Subject: Re: indexing within a list using Splus 2000 and Splus 6
From: Nick.Ellis@csiro.au
Date: Thu, 24 Oct 2002 10:48:48 +1000
Cc: s-news@lists.biostat.wustl.edu
>  
> -----Original Message-----
> From: Steve Su [mailto:s.su@qut.edu.au]
> Sent: Wednesday, October 23, 2002 5:41 PM
> To: s-news@lists.biostat.wustl.edu
> Subject: [S] indexing within a list using Splus 2000 and Splus 6
> Importance: High
> 
> 
> Dear All,
> 
> Thank you for all of you that have provided help to me in the 
> last few requests. Sorry if my questions are too juvenile for 
> you to be of any major challenge.
> 
> Again I have a very simple query, but I cannot seem to work 
> it out easily.here is my list.
> 
> A
> [[1]]:
> [[1]][[1]]:
> [[1]][[1]][[1]]:
>         start      dur   finish     
> [1,] 134.3004 62.00364 196.3040 5  7
> [2,] 255.6225 56.20274 311.8253 9 11
> [3,] 260.6033 92.38260 352.9859 9 12
> 
> [[1]][[1]][[2]]:
>         start        dur   finish    value    
> [1,] 195.3584 0.94558347 196.3040 2.830723 7 7
> [2,] 255.6225 3.11405361 258.7366 2.857876 9 9
> [3,] 260.6033 0.07717811 260.6805 3.201092 9 9
> 
> [[1]][[1]][[3]]:
>         start       dur   finish     value      
> [1,] 196.3040  3.732562 200.0366 -2.743207  7  7
> [2,] 311.8253  7.306538 319.1318 -2.696593 11 11
> [3,] 352.9859 11.687487 364.6734 -2.905473 12 12
> 
> 
> [[1]][[2]]:
> [[1]][[2]][[1]]:
>         start      dur   finish      
> [1,] 196.8454 23.33951 220.1849  7  8
> [2,] 280.6963 22.38124 303.0775 10 10
> 
> [[1]][[2]][[2]]:
>         start dur   finish    value      
> [1,] 215.1849   5 220.1849 2.948743  8  8
> [2,] 280.6963   5 285.6963 2.911219 10 10
> 
> [[1]][[2]][[3]]:
>         start dur   finish     value      
> [1,] 220.1849   5 225.1849 -3.115096  8  8
> [2,] 303.0775  10 313.0775 -2.969132 10 11
> 
> 
> I want to combine A[[1]][[1]][1] and A[[1]][[2]][1], and 
> similarly A[[1]][[1]][2] with A[[1]][[2]][2] , and lastly 
> A[[1]][[1]][3] with A[[1]][[2]][3]. .I am just puzzled why I 
> cannot use A[[1]][1:2][1] to get all the items in 
> A[[1]][[1]][1] and A[[1]][[2]][1]? 


A[[1]] is a list of 2 elements, each element being a list of 3 matrices.
Because A[[1]] has only 2 elements A[[1]][1:2] is the same thing.
A[[1]][1:2][1] is therefore a list of 1 element, that element being a list
of 3 matrices. "[" operating on lists always returns a list; "[[" returns
the element of a list.

One way to get the 1st element of the two components is

lapply(A[[1]], "[[", 1)

You can then combine them with do.call

do.call("rbind",lapply(A[[1]], "[[", 1))

BTW, another way of getting the first two elements is using a different kind
of indexing:

lapply(1:2,function(i) A[[1]][[c(i,1)]])

that is "[[" with a vector argument. For instance A[[c(1,2,3)]] is the same
as A[[1]][[2]][[3]]. 

To the s-news community:
I don't remember anyone using this form of indexing on s-news. I wonder if
anyone routinely uses this kind of indexing, and would like to share any
tricks? The feature is not described under help("[["); you have to go to the
Programmers' Guide, Chapter 4, section "Subscripting Lists".

Nick Ellis
CSIRO Marine Research   mailto:Nick.Ellis@csiro.au
PO Box 120                      ph    +61 (07) 3826 7260
Cleveland QLD 4163      fax   +61 (07) 3826 7222
Australia                       http://www.marine.csiro.au
> 
> Is there a way I can combine them without having to tediously 
> write out A[[1]][[1]][1] with A[[1]][[2]][1] etc..?
> 
> Thanks for your attention.
> 
> 
> 
> 
> **************************************************************
> ************************
> 
>  Steve Su (s.su@qut.edu.au)  
>  PhD student.
> 
>  School of Accountancy
>  School of Mathematical Sciences
>  Queensland University of Technology
> 
>  Postal Address: Steve Su, School of Accountancy, QUT, PO Box 
> 2434, Brisbane,
>  Queensland, Australia, 4000. 
> 
> 
>  Phone:  +61 7 3864 2017
>  Fax:    +61 7 3864 1812    
>  Mobile: 0421  840  586
>      .                             
>    _--_|\                
>   /      QUT                                 
>   \_.--._/                                                    
>                      
>         v                                             
>                                                                     
> **************************************************************
> ************************    
> 

<Prev in Thread] Current Thread [Next in Thread>