parse_netlogo_list() parses NetLogo-style lists represented as strings
(e.g., "[1 2 3]") into R lists. It automatically detects
numeric, integer,
logical, and character types
within the lists and converts them accordingly.
Note: We recommend using this function only when necessary, as it can
be computationally intensive for large datasets and may not handle all edge
cases. NetLogo provides a special output format called
lists
that exports list metrics in a tabular structure. If your experiment includes
metrics that return NetLogo lists, include "lists" in the outputs
argument of run_experiment() to capture this output.
Arguments
- x
An
atomicvector potentially containing NetLogo-style lists.
Value
A list where each element is the parsed result of
the corresponding input element. Parsed elements may be atomic vectors (for
homogeneous lists) or nested lists (for mixed-type or nested lists). If a
NetLogo list is not detected in an input element, that element is returned
as a single-element list containing the original string.
Details
The function handles the following cases:
Homogeneous lists: Lists containing elements of the same type are returned as atomic vectors (e.g.,
"[1 2 3]"becomesc(1L, 2L, 3L)).Mixed-type lists: Lists containing elements of different types are returned as R lists (e.g.,
'[1.1 "a" true]'becomeslist(1.1, "a", TRUE)).Nested lists: Lists containing other lists are fused with the main list (e.g.,
'["a" "b" [1 2]]'becomeslist(c("a", "b"), c(1L, 2L))).
NetLogo boolean values (true/false) are converted to R
logical values (TRUE/FALSE). NetLogo
NaN values are parsed as R NaN .
See also
Other parsing functions:
parse_netlogo_color()
Examples
# Scalar Examples -----
'test' |> parse_netlogo_list() # Not a NetLogo list.
#> [[1]]
#> [1] "test"
#>
'[1]' |> parse_netlogo_list()
#> [[1]]
#> [1] 1
#>
'["a" "b" "c"]' |> parse_netlogo_list()
#> [[1]]
#> [1] "a" "b" "c"
#>
'[1 2 3]' |> parse_netlogo_list()
#> [[1]]
#> [1] 1 2 3
#>
'[1.1 2.1 3.1]' |> parse_netlogo_list()
#> [[1]]
#> [1] 1.1 2.1 3.1
#>
'[true false true]' |> parse_netlogo_list()
#> [[1]]
#> [1] TRUE FALSE TRUE
#>
# Vector Examples -----
c(1, 2, 3) |> parse_netlogo_list() # Not a NetLogo list.
#> [[1]]
#> [1] 1 2 3
#>
c('["a" "b" "c"]', '["d" "e" "f"]') |> parse_netlogo_list()
#> [[1]]
#> [1] "a" "b" "c"
#>
#> [[2]]
#> [1] "d" "e" "f"
#>
c('[1 2 3]', '[4 5 6]') |> parse_netlogo_list()
#> [[1]]
#> [1] 1 2 3
#>
#> [[2]]
#> [1] 4 5 6
#>
c('[1.1 2.1 3.1]', '[4.1 5.1 6.1]') |> parse_netlogo_list()
#> [[1]]
#> [1] 1.1 2.1 3.1
#>
#> [[2]]
#> [1] 4.1 5.1 6.1
#>
c('[true false true]', '[false true false]') |> parse_netlogo_list()
#> [[1]]
#> [1] TRUE FALSE TRUE
#>
#> [[2]]
#> [1] FALSE TRUE FALSE
#>
# Mixed-Type Examples -----
'["a" "b" 1 2]' |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] "a"
#>
#> [[1]][[2]]
#> [1] "b"
#>
#> [[1]][[3]]
#> [1] 1
#>
#> [[1]][[4]]
#> [1] 2
#>
#>
'[1.1 2.1 3.1 true false]' |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] 1.1
#>
#> [[1]][[2]]
#> [1] 2.1
#>
#> [[1]][[3]]
#> [1] 3.1
#>
#> [[1]][[4]]
#> [1] TRUE
#>
#> [[1]][[5]]
#> [1] FALSE
#>
#>
'[1.1 "a" true]' |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] 1.1
#>
#> [[1]][[2]]
#> [1] "a"
#>
#> [[1]][[3]]
#> [1] TRUE
#>
#>
# Nested Examples -----
'["a" "b" "c" [1 2]]' |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] "a" "b" "c"
#>
#> [[1]][[2]]
#> [1] 1 2
#>
#>
'["a" "b" "c" [1 2] true ["d" "c"]]' |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] "a" "b" "c"
#>
#> [[1]][[2]]
#> [1] 1 2
#>
#> [[1]][[3]]
#> [1] TRUE
#>
#> [[1]][[4]]
#> [1] "d" "c"
#>
#>
'[[1 2] [3 4] [5 6]]' |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] 1 2 3 4 5 6
#>
#>