加载重复项/合并变量
Loading duplicates/ merging variables
我正在加载包含大量重复项的动物数据,我试图将这些重复项合并到一个代表单个动物的代理中。 csv 文件如下所示:
动物 ID - 组 ID
1 - A
2 - A
3 - A
4 - A
1 - B
2 - B
对于这个例子,我希望生成 4 个独特的动物代理,它们有一个与它们相关的组的列表。动物 1 的列表为 [A, B],动物 4 的列表为 [A].
到目前为止,我正在使用以下方法加载 csv:
csv:from-row file-read-line
create-animal 1 [
set Animal-ID item 0 data
set group-ID item 1 data]
Which produces 6 animals with one group id each.
But how should I cull the duplicate animals?
如果您的 csv 如下所示:
id, group
1,A
2,A
3,A
4,A
1,B
2,B
您可以将 csv 作为列表加载,提取独特的动物,然后使用独特的动物 ID 过滤原始列表以获取该动物所属的独特组:
extensions [ csv ]
breed [ animals animal ]
animals-own [ animal-id group-id ]
to setup
ca
; Load animal data as a list of lists, drop the headers
let animalData but-first csv:from-file "exampleAnimals.csv"
print animalData
; Get the unique animal ids
let animalIds remove-duplicates map [ i -> first i ] animalData
print animalIds
foreach animalIds [ id ->
create-animals 1 [
; Set the id
set animal-id id
; Filter the animal data by the id of the current animal
let filtered filter [ row -> first row = id ] animalData
; Map to pull the group id as a list and assign to the animal
set group-id map [ i -> last i ] filtered
fd 1
]
]
reset-ticks
end
希望对您有所帮助!
我正在加载包含大量重复项的动物数据,我试图将这些重复项合并到一个代表单个动物的代理中。 csv 文件如下所示:
动物 ID - 组 ID
1 - A
2 - A
3 - A
4 - A
1 - B
2 - B
对于这个例子,我希望生成 4 个独特的动物代理,它们有一个与它们相关的组的列表。动物 1 的列表为 [A, B],动物 4 的列表为 [A].
到目前为止,我正在使用以下方法加载 csv:
csv:from-row file-read-line
create-animal 1 [
set Animal-ID item 0 data
set group-ID item 1 data]
Which produces 6 animals with one group id each.
But how should I cull the duplicate animals?
如果您的 csv 如下所示:
id, group
1,A
2,A
3,A
4,A
1,B
2,B
您可以将 csv 作为列表加载,提取独特的动物,然后使用独特的动物 ID 过滤原始列表以获取该动物所属的独特组:
extensions [ csv ]
breed [ animals animal ]
animals-own [ animal-id group-id ]
to setup
ca
; Load animal data as a list of lists, drop the headers
let animalData but-first csv:from-file "exampleAnimals.csv"
print animalData
; Get the unique animal ids
let animalIds remove-duplicates map [ i -> first i ] animalData
print animalIds
foreach animalIds [ id ->
create-animals 1 [
; Set the id
set animal-id id
; Filter the animal data by the id of the current animal
let filtered filter [ row -> first row = id ] animalData
; Map to pull the group id as a list and assign to the animal
set group-id map [ i -> last i ] filtered
fd 1
]
]
reset-ticks
end
希望对您有所帮助!