Very new to coding. I have been attempting to input values from a for loop iteration into a dataframe. I have looked at many asked questions however they did not seem to help my situation. Once my for loop is complete, my cat() outputs all values of the iteration. I have tried to create a dataframe using the names of the variables as you can see in the code however it only outputs one row with the last iteration. I have been told to create a dataframe before my for-loop in order to store the output values, but I cannot seem to figure it out. If anyone can show me the correct code that would be very greatly appreciated! Thank you
Numyears<- 0
NumLynxes<-0
NumHares<-0
flag<- F
NumBabyLynxes<-0
NumBabyHares<-0
NumDeadHares<-0
NumDeadLynxes<-0
NumHaresEaten<-0
while(flag !=T) {
ask <-readline(prompt = "Simulate the population of Lynx and Hares in Canada?
Type 1 for yes, Type anything else to exit:")
response<- as.integer(ask)
if (response ==1) {
variable1 <- readline(prompt = "Input the number of years for the simulation sample:")
Numyears <- as.integer(variable1)
variable2 <- readline(prompt="Enter the number of Lynxes in this simulation:")
NumLynxes <- as.integer(variable2)
variable3 <-readline("Enter the number of Hares in this simulation:")
NumHares <-as.integer(variable3)
DF <- data.frame(Numyears,NumHares,
NumLynxes,NumBabyHares,
NumBabyLynxes,
NumDeadHares,
NumDeadLynxes,
NumHaresEaten,stringsAsFactors = F)
for (years in 1:Numyears) {
if ( (NumLynxes/NumHares) < 0.20) {
NumDeadLynxes<-round(NumLynxes * 0.02)
NumBabyLynxes <- round(0.15*NumLynxes)
}
if ((NumLynxes/NumHares) >= 0.20) {
NumDeadLynxes <- round(0.50*NumLynxes)
NumBabyLynxes <- round(0.15*NumLynxes)
}
NumBabyHares<- round(0.75*NumHares)
NumDeadHares <- round(NumDeadHares*0.01)
NumHaresEaten <- round((NumLynxes*NumHares)*0.025)
years <- years
NumLynxes <- ((NumLynxes + NumBabyLynxes) - NumDeadLynxes)
NumHares <- ((NumHares + NumBabyHares) - (NumHaresEaten + NumDeadHares))
if ((NumLynxes/NumHares) < 0){
break
}
cat("Year #Hares #Lynxes babyH babyL deadHare deadLynx","\n",
years," ","\t",NumHares," ","\t",NumLynxes, " ","\t",NumBabyHares,
" ","\t",NumBabyLynxes," ","\t",NumDeadHares," ","\t",NumDeadLynxes,
" ", "\t",NumHaresEaten)
}
} else{
flag <- T
}
}
DF