Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将for循环的输出添加到data.frame中,这样我就可以在r中使用ggplot2绘制它们_R_Dataframe_For Loop_Simulation - Fatal编程技术网

将for循环的输出添加到data.frame中,这样我就可以在r中使用ggplot2绘制它们

将for循环的输出添加到data.frame中,这样我就可以在r中使用ggplot2绘制它们,r,dataframe,for-loop,simulation,R,Dataframe,For Loop,Simulation,我想计算一个食饵-捕食者系统在一个一致的时间段后的人口数量,为此,我根据以下公式创建了我的代码: r中的代码: matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix basepop <- matrix(c(10,30),nrow = 2) #base population plot(basepop[1,1],basepop[2,1]) for(k in 1:50){

我想计算一个食饵-捕食者系统在一个一致的时间段后的人口数量,为此,我根据以下公式创建了我的代码:

r中的代码:

matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2)            #base population

plot(basepop[1,1],basepop[2,1])
for(k in 1:50){
population <- matrixA %*% basepop  #population[1,2] - predator
basepop <- population              #population[2,1] - prey
  print(population)

}

matrixA您可以将总体矩阵的每次迭代添加到数据框中进行绘图:

matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2)            #base population

plot(basepop[1,1],basepop[2,1])
continued.df <- data.frame("predator" = rep(NA, times = 50), "prey" = rep(NA, times = 50))
for(k in 1:50){
  population <- matrixA %*% basepop  #population[1,2] - predator
  basepop <- population       #population[2,1] - prey
  print(population)
  continued.df$prey[k] <- population[2]
  continued.df$predator[k] <- population[1]
}
plot(continued.df)

matrixA您可以将总体矩阵的每次迭代添加到数据框中进行绘图:

matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2)            #base population

plot(basepop[1,1],basepop[2,1])
continued.df <- data.frame("predator" = rep(NA, times = 50), "prey" = rep(NA, times = 50))
for(k in 1:50){
  population <- matrixA %*% basepop  #population[1,2] - predator
  basepop <- population       #population[2,1] - prey
  print(population)
  continued.df$prey[k] <- population[2]
  continued.df$predator[k] <- population[1]
}
plot(continued.df)

matrixA与@JustinCocco的答案相似,但有点不同

  • 我使总体成为一个矩阵,列数等于模拟周期数
  • 完成模拟后,我将转换为数据帧

  • matrixA与@JustinCocco的答案相似,但有点不同

  • 我使总体成为一个矩阵,列数等于模拟周期数
  • 完成模拟后,我将转换为数据帧

  • matrixA哦,这是一个很好的、很好的添加k值的方法。非常感谢你们两位的快速回答。如果你们不介意的话,可以告诉我如何将图形居中到0,这样它就可以显示负x、y轴。你可以用例如
    coord\u笛卡尔
    这样的方法设置限制:
    +coord\u笛卡尔(xlim=c(-1,1)*max(种群密度$捕食者),ylim=c(-1,1)*最大值(种群密度$猎物))
    。这是一个很好的添加k值的方法。非常感谢你们两位的快速回答。如果你不介意的话,你能告诉我如何将图形居中到0,这样它可以同时显示负x,y轴。你可以使用例如
    坐标笛卡尔
    这样的方法设置限制:
    +坐标笛卡尔(xlim=c(-1,1)*max(人口df$predator),ylim=c(-1,1)*最大(种群密度$prey))