[R] From replicate to accesing sublists

David Winsemius dwinsemius at comcast.net
Thu Apr 2 19:13:59 CEST 2015


On Apr 1, 2015, at 9:59 PM, Alaios wrote:

You are still replying with formatted email. This mailing list attempts to be an HTML-free zone.

> Thanks.The code you gave me at the end works correctly.. I was wondering if there is more efficient way to access each element withouth this classic for loop.

I don't think we can answer that unless we have a better understanding of the goals of this effort and the uses to which the data will be put.

If you used apply() you could work on an entire row or column at a time.

You could disguise the loop with no gain in time efficiency with sapply().

Shadows <- sapply(error_suburban_0, "[[", 'Shadowing') # saves you the typing effort of not calculating the length of the object; cleaner syntax.

There is a misconception that refuses to die, despite being smacked down many,many times on Rhelp, that loops in R are inefficient. It is usually the algorithm inside the loop that is inefficient.

-- 
David.

> 
> Regards
> Alex
> 
> 
> 
> On Wednesday, April 1, 2015 5:54 PM, David Winsemius <dwinsemius at comcast.net> wrote:
> 
> 
> 
> On Apr 1, 2015, at 3:34 AM, Alaios via R-help wrote:
> 
> > Dear all,I have a R structure that was created with replicate.The data sets looks to be a matrix with each cell being a list.
> > str(error_suburban_0[,1],max.level=1)
> > List of 4
> > $ vaR      :List of 20
> >  ..- attr(*, "class")= chr "variogram"
> > $ Shadowing:List of 2
> >  ..- attr(*, "class")= chr "geodata"
> > $ FIT      :List of 1
> > $ propmodel:List of 12
> >  ..- attr(*, "class")= chr "lm"
> > 
> > 
> > The error_suburban is a matrix that each field so 
> > error_suburban_0[,1], error_suburban_0[,2], error_suburban_0[,3], error_suburban_0[,4],... and so on,  contains the four sublists
> > $ vaR      :List of 20
> >  ..- attr(*, "class")= chr "variogram"
> > $ Shadowing:List of 2
> >  ..- attr(*, "class")= chr "geodata"
> > $ FIT      :List of 1
> > $ propmodel:List of 12
> >  ..- attr(*, "class")= chr "lm"
> > 
> > 
> > I would like to pick for each of these matrix elements to collect only the $Shadowing sublist
> > error_suburban_0[,1], error_suburban_0[,2], error_suburban_0[,3], error_suburban_0[,4]... and so on
> > 
> > Right now I am implementing this by a for loop that access each matrix element sequentially.
> 
> It would have been better to show the results of dim() or dput(). Matrix objects (which are capable of holding lists) should be accessible with either a single or double argument to "[". This should deliver contents:
> 
> for (i in 1:4)  print(  error_suburban_0[i]$Shadowing )
> 
> If the matrix has 4 or more rows, then that would be accessing only from the first column. If fewer than 4 rows, you would be wrapping around to later columns.
> 
> -- 
> David.
> 
> 
> > 
> > Can you please advice me if there is a better approach to do that in R?
> > Regards
> > Alex
> >  
> > 
> >     [[alternative HTML version deleted]]
> 
> 
> This is a plain text mailing list. Please reconfigure your email client to sent in plain text.
> 
> -- 
> 
> David Winsemius
> Alameda, CA, USA
> 
> 
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list