Skip to content

Help me create a hyper-structure

7 messages · Alaios, Ben Bolker, Duncan Murdoch

#
Dear all 
I would like to have in R a big struct containing a smaller struct. 

I have tried something that I would like to share and ask your help make it work


1) I would like to have a small struct with the following three fields
xorder (an integer ranging from 0 to 20)
yorder (an integer ranging from 0 to 20)
estimated (a 256*256 matrix)

2) I would like to have 10 elements of the struct above
for that I wrote the following:



1) Estimationstruct <- function ( xorder, yorder, estimated) list (xorder= xorder, yoder=yorder,estimated=estimated) 

2) per.sr.struct <-replicate(10,Estimationstruct(vector(mode="integer",length=1),vector(mode="integer",length=1), matrix(data=NA,nrow=256,ncol=256)),simplify=FALSE)


That one worked.
per.sr.struct contains 10 elements and each one of that contains 1).
This works fine:

str(per.sr.struct[[1]])
List of 3
 $ xorder       : int 0
 $ yoder        : int 0
 $ estimated: logi [1:256, 1:256] NA NA NA NA NA NA ..


Then I would like
3) to have 20 times the number two. 
For that I tried out
all.sr.struct<-replicate(20,per.sr.struct)
The idea is to have 20 all.sr.stuct and each element to contain one per.sr.struct.

What is wrong with my reasoning?

Best Regards
Alex

but I am not sure if this works correctly.
I have tried somet
#
Alaios <alaios <at> yahoo.com> writes:
Estimationstruct <- function ( xorder, yorder, estimated) {
  list (xorder= xorder,
yorder=yorder,estimated=estimated)
}

per.sr.struct <- replicate(10,
             Estimationstruct(0L,0L,matrix(nrow=256,ncol=256)),
   simplify=FALSE)
all.sr.struct <-   replicate(20,per.sr.struct,simplify=FALSE)
I think you just missed simplify=FALSE in the last step ...
#
Dear Ben,
I would like to thank you for your answer.

Unfortunately is not very clear how simplify works and what is the difference between TRUE and FALSE ( I am not at work right now to check the difference)

Regards
Alex
--- On Sat, 4/16/11, Ben Bolker <bbolker at gmail.com> wrote:

            
1 day later
#
It seems you were right.
Now I can easily access my struct and substruct like this

# all.str[[1]]] Gives access to the first struct of per.sr.struct which containts 101 times the xorder,yorder,estimation.sr
# all.str[[1]][[2]] Gives access to the second substruct of all.str[[1]]
# all.str[[1]][[2]][[3]] Gives access to the matrix.

Now I would like to ask you if in R cran I can make struct assignments like this 


    all.str[[i]]<-TempApproxstruct


where all.str[[i]] is  a list that contains 100 times the
 $ :List of 3
  ..$ xorder       : int 0
  ..$ yoder        : int 0
  ..$ estimation.sr: logi [1:256, 1:256] NA NA NA NA NA NA ...
 $ :List of 3
  ..$ xorder       : int 0
  ..$ yoder        : int 0
  ..$ estimation.sr: logi [1:256, 1:256] NA NA NA NA NA NA ...
.... and so on

where str(temp.per.sr.struct) is a list that contains 100 times the
 $ :List of 3
  ..$ xorder       : int 0
  ..$ yoder        : int 0
  ..$ estimation.sr: logi [1:256, 1:256] NA NA NA NA NA NA ...
 $ :List of 3
  ..$ xorder       : int 0
  ..$ yoder        : int 0
  ..$ estimation.sr: logi [1:256, 1:256] NA NA NA NA NA NA ...
  [list output truncated]
...and so on.

Will R understand this kind of assignments or not?

I would like to thank you in advance for your help
Best Regards
Alex
--- On Sat, 4/16/11, Ben Bolker <bbolker at gmail.com> wrote:

            
#
On 11-04-18 04:45 AM, Alaios wrote:
Why don't you just try it and see what happens ... ?
#
On 18/04/2011 4:45 AM, Alaios wrote:
Something that may not be obvious is that

all.str[[1]][[2]][[3]]


can also be written as

all.str[[c(1,2,3)]]


This is useful when the structure is an irregular shape, because the vector c(1,2,3) could be stored in a variable, and on the next call it could have a different length.

Be careful though:  all.str[c(1,2,3)] (with single brackets) means something quite different:  it means

list(all.str[[1]], all.str[[2]], all.str[[3]])

i.e. a subset of the top level indices.

Duncan Murdoch
#
Thank you very much . That was really helpful.
I will keep this email for future reference

Regards
--- On Mon, 4/18/11, Duncan Murdoch <murdoch.duncan at gmail.com> wrote: