Equivalent of list() (R) in Julia

Dear all,

Quite new in Julia (I’m already a big fan), I was wondering if there exist any data structure in Julia like “list()” in R. I search in the documentation, on google. Maybe I didn’t search well but I didn’t find anything.

I’m sorry if this a recurrent post.

Thank you very much and have a nice day

You mean a list with objects of different types? This is the default in Julia:

a = ["Julia is", 100, '%', :cool]

You can use a vector as a list – you can do all of the standard dequeue operations on it, including push!, pop!, shift! and unshift!.

3 Likes

Ok thank you for that.

But I also think to have something like

list_a
$A
$A$a
some_data_a
$A$b
some_other_data_a
$B
$B$a
some_data_b
$B$b
some_other_data_b

I can do recursively vector with data, but is there a way after to access my data with name, like

list_a$B$b
some_other_data_b

Thank you very much

1 Like

And I agree with

a = [“Julia is”, 100, ‘%’, :cool]

:slight_smile:

There’s no exact equivalent, but a Dict (or an OrderedDict from package DataStructures if order matters) is probably the closest structure.

1 Like

You could use a Vector of types:

type myt
    a::Matrix{Float64}
    b::String
end

list = [myt(randn(10,10),"some_data"),
    myt(rand(5,5),"other_data") ]

list[1].b
list[2].b

So elements of the type named, and elements of the Vector are indexed by an integer. Note the keyword changes to types in Julia 0.6.

Yes an OrderedDict should be the same as an R list, except that you cannot index it with numbers.

@Jonathan_Droxler: The list object in R is very similar to Dict() in Julia. I have been fairly using Dict() in Julia to transfer my R list objects to Julia.

1 Like

Thank you very much.

Dict() is indeed very usefull.

Thank you and have pleasure with Julia :wink:

You may want to look at the RCall package as one of its purposes is to map R objects to Julia types and vice-versa. It even allows you to enter R code in the Julia REPL for evaluation in an inferior R process. (The word “inferior” is not a value judgement here, it means that the Julia process has started and controls and R process).

In this transcript, one switches to the R> prompt by typing a $ at the julia> prompt. The backspace switches back to julia>.

julia> using RCall
INFO: Using R installation at /usr/lib/R

R> mylist <- list(A = list(a=1L, b="foo"), B=list(aa=2.14, b="baz"), C = "boz")

R> str(mylist)
List of 3
 $ A:List of 2
  ..$ a: int 1
  ..$ b: chr "foo"
 $ B:List of 2
  ..$ aa: num 2.14
  ..$ b : chr "baz"
 $ C: chr "boz"

julia> rcopy(R"mylist")
Dict{Symbol,Any} with 3 entries:
  :A => Dict{Symbol,Any}(Pair{Symbol,Any}(:a, 1),Pair{Symbol,Any}(:b, "foo"))
  :B => Dict{Symbol,Any}(Pair{Symbol,Any}(:b, "baz"),Pair{Symbol,Any}(:aa, 2.14))
  :C => "boz"
4 Likes

While named R list is like Dict or OrderedDict.
Unnamed R list is not exactly Dict as order of the members matters. It is more like Vector{Any}.

Or you can mix named and unnamed:

list(1, a=2, b=3) # R code

There is no equivalent of R’s list() in Julia, but since like most R constructs it is a DWIM monster that would lead to horrible code in Julia (total lack of type stability, etc), it may be for the best.

Generally it is better to ask about solving a specific problem (“I want to do …”) instead of asking about the equivalent of something in another language, since besides trivial cases, there isn’t one, so respondents try to guess what the OP wanted.

3 Likes

Just to give a full answer - R and Julia divides their containers along different criteria, so it is hard to talk about strict equivalence. Here is a short review:

R divides the main container type by heterogeneity: list takes objects of any type and allows for different types, vector takes strings and primitives and only objects of the same type. In addition to this definition there are some behavioral differences: list support indexing by name with the $, vector also supports named indexing but in a different way (the bit in italics has been edited based on replies below). And indexing into a list always returns a list - returning a list element requires double indexing ([[]]), whereas indexing into a vector with a scalar returns an element (that is because scalars in R are by definition just length-1 vectors). Finally, list doubles as user types - a user type (S3) is just a list with certain named fields.

In julia, these divisions are orthogonal and independent. All types are treated equal, so all containers can take both user types and primitives (primitives can also be user types). Whether elements are the same or different types is signified by the Type Parameter, i.e. is it a Vector{Int} or a Vector{Any}, for any container type. Container types are divided by behavior: The division between Dicts and Vectors is whether you access by name or by a numeric index (and frankly in R you rarely use both on the same object). Indexing a container with a Vector also gives a Vector, whereas indexing with a scalar returns an element. Which doesn’t cause problems in applications, because indexing with a length-1 Vector will not be interpreted as a scalar (which is the issue double indexing in R seeks to avoid). Finally user types have their own, powerful, system.

A common use case for lists in R is to return multiple values from a function. In Julia, you’d either build a user type (if you want to keep them together) or return the values in a Tuple (for ad hoc use).

So that is why you cannot say which julia type is equivalent to a list - I personally think you will prefer julia’s system once you get acquainted with it.

7 Likes
> A = c("a"=1,"b"=2)
> str(A)
 Named num [1:2] 1 2
 - attr(*, "names")= chr [1:2] "a" "b"
> A["a"]
a
1
> A["b"]
b
2

:slight_smile:

1 Like

Thats funny - I use named vectors in R all the time and have never used the names for indexing. I wonder if this was added after I learnt R… I seem to not be the only one who has loosely associated R lists with julia Dicts because of the named indexing :slight_smile:

1 Like

Indexing by name has been in R before there was an R. R is based on an earlier language S developed at Bell Labs and indexing by name was part of S.

All user-visible R objects can have “attibutes”, one of which can be names.

2 Likes

Just an oversight then :slight_smile: Thanks for pointing this out. I wonder how often vector indexing by name is used in the wild?

Also @Jonathan_Droxler, one other aspect of Dicts that it is good for people coming from R to be aware of is that they don’t need to be indexed with a ‘name’, i.e. indexes can be any type, not just a string or symbol. I find that useful surprisingly often.

Oh and maybe finally worth mentioning that named arrays are possible in Julia with the NamedArrays.jl package.

I think this is maybe still the best answer. I was thinking, there must be sth like ‘NamedArrays.jl’ - however, one thing Julia does not emulate are attributes in R which any object can have (it is very Common Lisp (CL) style - where there are property lists assignable to any object - even single symbols - some people might not like it but there is meta-info storable - so actually quite cool for metaprogramming). Probably to imitate fully R’s lists, one should create custom structs in Julia …