I am trying to create a nested dictionary starting from a data.frame.
dta <- data.frame(x = c('A','B','A','B'),
y = c('P','Q','Q','P'),
z = c(89,12,34,56)
) |> print()
#> x y z
#> 1 A P 89
#> 2 B Q 12
#> 3 A Q 34
#> 4 B P 56
The following is not what I am looking for.
as.list(dta)
#> $x
#> [1] "A" "B" "A" "B"
#> $y
#> [1] "P" "Q" "Q" "P"
#> $z
#> [1] 89 12 34 56
What I am trying to do is to create a lookup table using make_nested_list
such that
nested_dta = make_nested_list(dta)
and nested_dta['A']['P']
will output 89
.
This answer is close but not useful for my case.
I am trying to do this in base R so that it is more readable. dplyr
can do this but seems wordy library(tidyvserse);dta %>% filter(x=='A',y=='P') %>% pull(z)
. There are other use cases that may benefit from the base R formulation. Is there is a better way to create the nested dictionary? Thank you.