mahagadorn / r-world-mahagadorn

r-world-mahagadorn created by GitHub Classroom
0 stars 0 forks source link

HELP ME PLEASE :( #5

Open mahagadorn opened 7 years ago

mahagadorn commented 7 years ago

Hi Will,

I am stuck, like stuck stuck. I have be wracking my brain about this all morning. I am tinkering here and there and it doesn't seem to work. I can't make sense of how to fix my error message.

It seems like my survive function is working. I have it printing i,j,k values and it looking like it recognizes those. When I put in a specific row,col,depth into the just the survive function it's doing what it should be doing. My issue is in the run.ecosystem function. I have it seeding just fine (not within the function, when just running this separate), however, I keep getting the error message


Error in plants[i, j, k] <- survive(plants[i, j, k], info) : 
  number of items to replace is not a multiple of replacement length

I am sorry, I am attempting to do this without your assistance, but like I said I am STUCK. My code is below, any help would be greatly appreciated!


plant.timestep <- function(plants, info){
  #define survivor function
  survive <- function(cell, info){
    if(is.na(cell))     #if it isnt a species I want you to return what is already in the contents of the cell
      return(NA)
    if(cell=='')
      return('')
    if(runif(1) <= info$survive[cell])   #your value is greater than or equal to your survival probability then you win yay!
      return(cell)
    if(runif(1) >= info$survive[cell])   #if the random number is greater that our survival probablity the return a blank space
      return('')    #this makes sense because if it dies it's no longer there...there is nothing in this cell
  }
  #looping through the plant matrix
  for(k in 1:(dim(plants)[3])){
    for(i in 1:dim(plants)[1]){
      for(j in 1:dim(plants)[2]){
      plants[i,j,k] <- survive(plants[i,j,k], info)
      print(c(i,j,k))
      }
    }
  }
  return(plants)
}

plant.timestep(plants, info)

#run.plant.ecosystem
run.plant.ecosystem <- function(terrain, num.timesteps, info){
  #Make the array
  plants <- array("", dim=c(dim(terrain), num.timesteps + 1))
  # for(i in 1:(.5*(nrow(terrain)*ncol(terrain))))   #######This doesn't work
  #below is how you RANDOMLY SEED YOUR PLANT MATRIX!!!!
  for(k in 1:(.5*nrow(terrain)^2)){
    plants[sample(nrow(plants),1), sample(ncol(plants),1), 1] <- sample(info$name, 1)
  }
  for(k in seq_len(dim(plants)[3])){
    #seq_len(y) or in our case (seq_len(dim(plants)) is creating a sequence up dimensions of plants array
    plants[,,k][is.na(terrain)] <- NA
  }
    for(k in 1:(dim(plants)[3])){
    plants[,,k] <- plant.timestep(plants, info)
  }
    return(plants)
}

run.plant.ecosystem(Test.Terrain, 3, info)
willpearse commented 7 years ago

Your error message means that you are trying to replace something with something that isn't of the same length as its replacement.

Take a look at the value of the thing you are trying to store (survive(plants[i, j, k], info)) and comparing it with the thing you're trying to store it into (plants[i, j, k]). I think if you look at the return statement on your function it will make more sense.