Dictionary

hello, I’m new to Julia, and here’s my problem:
using RDatasets, I wanted to make a dictionary of the first 2columns of the “iris” data frame; here’s how I did it:

using RDataets
IRIS = dataset(“datasets”,“iris”)
j = Dict()
for i in 1:length(IRIS[!,1])
j[IRIS[i,1]]=IRIS[i,2]
end
but given that number of the entries must be 150 (there are150 rows in “iris” dataset), the dicitonary has only 35 entries:
julia> j
Dict{Any,Any} with 35 entries:
5.5 => 2.6
4.3 => 3.0
6.5 => 3.0
7.7 => 3.0
4.5 => 2.3
5.9 => 3.0
7.1 => 3.0
7.0 => 3.2
7.4 => 2.8
4.4 => 3.2
5.7 => 2.5
6.7 => 3.0
6.2 => 3.4
4.7 => 3.2
6.4 => 3.1
6.6 => 3.0
6.0 => 3.0
4.6 => 3.2
6.1 => 2.6
6.8 => 3.2
6.3 => 2.5
5.8 => 2.7
6.9 => 3.1
5.1 => 2.5
5.2 => 2.7
5.3 => 3.7
7.6 => 3.0
4.8 => 3.0
5.0 => 2.3
5.6 => 2.8
7.3 => 2.9
4.9 => 2.5
7.2 => 3.0
5.4 => 3.0
7.9 => 3.8

what did i do wrong? thank you.

The first column of IRIS only has 35 unique values. Consequently, you are overwriting values in the Dict as you assign them.

See the following

julia> d = Dict();

julia> d["a"] = 1
1

julia> d["a"] = 2
2

julia> d
Dict{Any,Any} with 1 entry:
  "a" => 2
3 Likes

wow! thankyou! I appreciate it.

1 Like