For loop 返回代理集';从循环将输出转换为数据帧
这是一个RNetlogo问题。假设我在Netlogo中有一个代理集,每个农民代表一个补丁。我在补丁上有一些“设置”和“运行”程序。我想买10克。在每次勾选时,我都希望针对代理商(农民或补丁)的一些变量提取R中的值。以下是我的有限参数代码-For loop 返回代理集';从循环将输出转换为数据帧,for-loop,netlogo,rnetlogo,For Loop,Netlogo,Rnetlogo,这是一个RNetlogo问题。假设我在Netlogo中有一个代理集,每个农民代表一个补丁。我在补丁上有一些“设置”和“运行”程序。我想买10克。在每次勾选时,我都希望针对代理商(农民或补丁)的一些变量提取R中的值。以下是我的有限参数代码- for(i in 10){ NLCommand("set CropPirce ", 16, "setup") NLDoCommand(i, "go") print(NLGetAgentSe
for(i in 10){
NLCommand("set CropPirce ", 16, "setup")
NLDoCommand(i, "go")
print(NLGetAgentSet(c("ticks", "pxcor", "pycor", "Profit"),
"patches with [a? = TRUE]")) # a? means if farmer adopted the crop
}
现在,如何将每个刻度步骤的打印值提取到数据框中
提前感谢。如果您想存储它,可以创建一个空数据帧,然后
rbind
每次调用NLGetAgentSet()
使用此测试模型:
to setup
ca
crt 3
reset-ticks
end
to go
ask turtles [
rt random 90 - 45
fd 1
]
tick
end
使您的数据帧为空:
vars <- c("ticks", "who", "pxcor", "pycor")
dfBase <- data.frame(matrix(
NA,
nrow = 0,
ncol = length(vars),
dimnames = list(NULL, vars)
))
非常感谢你,卢克!你的回答对我很有用。@KaziMasel-很高兴能帮上忙!快乐编码
NLCommand("setup")
for (i in 1:10) {
NLCommand("go")
dfBase <- rbind(dfBase, (NLGetAgentSet(vars, 'turtles')))
}
> head(dfBase); tail(dfBase)
ticks who pxcor pycor
1 1 0 -1 0
2 1 1 -1 -1
3 1 2 -1 1
4 2 0 -2 -1
5 2 1 -1 -2
6 2 2 -2 1
ticks who pxcor pycor
25 9 0 -8 -2
26 9 1 -1 -7
27 9 2 -7 3
28 10 0 -9 -2
29 10 1 0 -7
30 10 2 -8 2