How do I use a map in R?
2 answers
Use a list, because a simple vector constructed with c
cannot handle anything more than scalar values:
> map = c(key1 = c(1,2,3), key2 = 2, key3 = 3)
> map[["key1"]]
Error in map[["key1"]] : subscript out of bounds
why is it failing? because map
now:
> map
key11 key12 key13 key2 key3
1 2 3 2 3
use list
instead:
> map = list(key1 = c(1,2,3), key2 = 2, key3 = 3)
> map[["key1"]]
[1] 1 2 3
also dynamically expandable:
> map[["key99"]]="Hello You!"
> map
$key1
[1] 1 2 3
$key2
[1] 2
$key3
[1] 3
$key99
[1] "Hello You!"
Start blank map=list()
if you are creating one.
+9
source to share