Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Dictionary in R

Tags:

dictionary

r

I'm more used to a python environment. Is there a way to produce a dictionary/list that is fully dynamic. I.e. that I could create flexible data structures say that relate to a particular number such as

[1:["var_a":6, "var_b":3],2:[..]]

Where I will not know how may elements in each list. Is there a way to do this?

like image 557
disruptive Avatar asked Mar 20 '23 10:03

disruptive


2 Answers

> a <- list()
> a[[1]] <- list(var_a=6)
> a[[1]]$var_b = 3
> a[[2]] <- list(var_c=8)

> a[[1]]
$var_a
[1] 6

$var_b
[1] 3    
> a[[1]]["var_b"]
$var_b
[1] 3
> a[[2]]$var_c
[1] 8
like image 126
Julián Urbano Avatar answered Apr 06 '23 07:04

Julián Urbano


As @JulianUrbano says, a list is a flexible data structure in R.

It's a vector:

l <- list()
is.vector(l)

That can take names:

l2 <- list( a = 1 , l = l )
l2[["a"]]
l2[1:2]
like image 32
Ari B. Friedman Avatar answered Apr 06 '23 08:04

Ari B. Friedman