Anonymous user
Associative array/Creation: Difference between revisions
add vector example
m (→version 2: corrected spelling of "capital". -- ~~~~) |
(add vector example) |
||
Line 1,235:
=={{header|R}}==
<lang R>a <- list(a=1, b=2, c=3.14, d="xyz")</lang>▼
R lacks a native representation of key-value pairs, but different structures allow named elements, which provide similar functionality.
=== vector example ===
<lang R>print(a$a) # [1] 1▼
print(a$d) # [1] "xyz"</lang> ▼
<lang r>> x <- c(hello=1, world=2, "!"=3)
<pre>hello world !
1 2 3</pre>
<pre>[1] "hello" "world" "!"</pre>
<pre>[1] 1 2 3</pre>
=== list example ===
▲<lang R>print(a[4])</lang>
[1] 1
$b
▲<pre>$d
[1]
$c
[1] 3.14
$d
▲<lang R>print(a[[4]])</lang>
[1] "xyz"</pre>
<lang r>> print(names(a))</lang>
<pre>[1] "a" "b" "c" "d"</pre>
<lang r>> print(unname(a))</lang>
<pre>[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3.14
[[4]]
[1] "xyz"</pre>
=={{header|Raven}}==
|