Skip to content
Prev 378432 / 398502 Next

[FORGED] Newbie Question on R versus Matlab/Octave versus C

On 1/29/2019 11:50 PM, Jeff Newmiller wrote:
Hi Jeff,

I'm well along in implementing your suggestions, but I don't understand 
the last paragraph. Here is part of the experimenting I've done so far:

*=======*=======*=======*=======*=======*=======*
updatePerson <- function() {
   ifelse( women$isAlive,
     {
# Check whether to kill off this person, if she's pregnant whether
# to give birth, whether to make her pregnant again.
       women$age = women$age + timeStep
# Check if the person has reached maxAge
     }
   )
}

calculatePopulation <- function() {
   lastDate = 0
   jd = 0
   while( jd < maxDate ) {
     for( i in seq_len( nWomen ) ) {
       updatePerson();
     }
     todaysDateInt = floor(jd/dpy)
     NAlive[todaysDateInt] = nWomen - nDead
# Do various other things
     todaysDate = todaysDate + timeStep
     jd = jd + timeStep
   }
}

nWomen <- 5
numberOfYears <- 30
women <- data.frame( isAlive = rep_len( TRUE, nWomen )
                    , isPregnant = rep_len( FALSE, nWomen )
                    , nChildren = rep_len( 0L, nWomen )
                    , ageInt = rep_len( 0L, nWomen )
                    , age = rep_len( 0, nWomen )
                    , dateOfPregnancy = rep_len( 0, nWomen )
                    , endDateLastPregnancy = rep_len( 0.0, nWomen )
                    , minBirthAge = rep_len( 0, nWomen )
                    , maxBirthAge = rep_len( 0, nWomen )
                    )

# . . .

   calculatePopulation()

*=======*=======*=======*=======*=======*=======*

The above code (in its complete form) executes without errors. I don't 
understand at least two things:

In the updatePerson function, in the ifelse statement, how do I change 
the appropriate values in the women dataframe?

I don't understand most of your last paragraph at all.

Thanks so much for your help in learning R!

Alan

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus