[R] From replicate to accesing sublists

Alaios alaios at yahoo.com
Thu Apr 2 06:59:06 CEST 2015


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.
RegardsAlex
 


     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


  
	[[alternative HTML version deleted]]



More information about the R-help mailing list