Dear all,
seasons greetings,
I had made a list of elements that I want to have 
# list generator
Agent<-list(id=NA, position=list(x=NA,y=NA))

# Make list of Agents
AgentList<-rep(list(CRAgent),5)


# Initialization. Is it shorter to do that with a lapply statement?
for (i in c(1:length(CRAgentList))){
  AgentList[[i]]$id=i
}




The created struct looks like
> str(AgentList)
List of 5
 $ :List of 3
  ..$ id                  : int 1
  ..$ position            :List of 2
  .. ..$ x: logi NA
  .. ..$ y: logi NA
  ..$ CRAgentPowerReceived: num [1:10, 1:10] 0 0 0 0 0 0 0 0 0 0 ...
 $ :List of 3
  ..$ id                  : int 2
  ..$ position            :List of 2
  .. ..$ x: logi NA
  .. ..$ y: logi NA
  ..$ CRAgentPowerReceived: num [1:10, 1:10] 0 0 0 0 0 0 0 0 0 0 ...


What I want to do is to initialize every struct with an id number. The first 
element I want to have the id number 1 the second id number 2 and so on.....


I have made that with a loop
for (i in c(1:length(CRAgentList))){
  AgentList[[i]]$id=i
}
but I was wondering If I can have also the same done with a lapply statement ( 
I prefer lapply so to convert it easily to mclapply).

After that I was also thinking to initialize the ...position$x  coordinates and 
position$y

bu assigning to every sub list the corresponding element of
CRX <- round(runif(nCR, min=0, max=nrow(r)))  # uniform distribute numbers 
between 0 and nrow(r)
CRY <- round(runif(nCR, min=0, max=nrow(r)))  # uniform distribute numbers 
between 0 and nrow(r)

so every agent should get
$id=number in the list
$position$x= CRX[number in the list]
$position$y= CRY[number in the list]

Is it possible to do these three initializations with one lapply statement? 


I would like to thank you in advance for your help
B.R
Alex
        [[alternative HTML version deleted]]

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

Reply via email to