lundi 5 octobre 2015

Assigning matrix with conditional ifelse function results in 0 value

I am trying to create a density-dependent Leslie matrix tool that loops the total population at a given time back into the formula and adjusts the rates of survival and/or births (i.e., Leslie matrix) for the next iteration (up to a given end point). The code below is embedded in the larger function, but I can see that there is something broken in this part. Note that I have pulled out only the relevant code for brevity, and checked to see that it behaved as in the large function.

When I run the code below, instead of the conditional ifelse function returning a matrix, it returns 0. The variable L.new should equal the 4 x 4 matrix L in the example.

How can I get the conditional to return a matrix, and why would this code return 0 instead of a matrix??

b.r <- c(0, 1, 5, 0)

surv.rates <- rbind( 
    age1 = c(0.3, 0.0, 0.0, 0.0),
    age2 = c(0.0, 0.6, 0.0, 0.0), 
    age3 = c(0.0, 0.0, 0.8, 0.0))

surv.rates2 <- rbind(     
    age1 = c(0.3/2, 0.0, 0.0, 0.0),
    age2 = c(0.0, 0.6/2, 0.0, 0.0), 
    age3 = c(0.0, 0.0, 0.8/2, 0.0))

surv.rates3 <- rbind(     
    age1 = c(0.3/4, 0.0, 0.0, 0.0),
    age2 = c(0.0, 0.6/4, 0.0, 0.0), 
    age3 = c(0.0, 0.0, 0.8/4, 0.0))

L  <- rbind(births = b.r, surv.rates)
L2 <- rbind(births = b.r, surv.rates2)
L3 <- rbind(births = b.r, surv.rates3)


t1 <- 10
t2 <- 100

L.new <- ifelse(1 > t2, yes = L3, 
                no = ifelse(1 > t1, yes = L2, 
                            no = L))
L.new # should equal matrix `L`


FYI Version info:

platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          2.2                         
year           2015                        
month          08                          
day            14                          
svn rev        69053                       
language       R                           
version.string R version 3.2.2 (2015-08-14)
nickname       Fire Safety 

Aucun commentaire:

Enregistrer un commentaire