I have an object in R that returns the following structure when I apply str to it:
str(x)
dist [1:1]
$ :List of 2
..$ mu : num 759
..$ sigma: num 11.2
..- attr(*, "class")= chr [1:2] "dist_normal" "dist_default"
@ vars: chr "Close"
I want to access the exact values of mu and sigma, but I cannot seem to find the correct way to do it... in the end I ended up using:
unlist(x)[1]
unlist(x)[2]
However, which would be the correct way to access the original list structure? Could somebody help?
As requested, the output of dput(x)
is:
dput(x)
structure(list(structure(list(mu = 758.880005, sigma = 11.1895832820955), class = c("dist_normal",
"dist_default"))), vars = "Close", class = c("distribution",
"vctrs_vctr", "list"))
With this you should be able to reproduce the object as follows:
x <- structure(list(structure(list(mu = 758.880005, sigma = 11.1895832820955), class = c("dist_normal",
"dist_default"))), vars = "Close", class = c("distribution",
"vctrs_vctr", "list"))
Just for your information I have loaded the package fpp3
for time series to handle these objects.
Thanks in advance.
CodePudding user response:
There is operators $
and [[
to extract the nested value by symbol name or character name, respectively. These operators must be used multiple times sequentially in case the list is nested:
x <- structure(list(structure(list(mu = 758.880005, sigma = 11.1895832820955), class = c("dist_normal", "dist_default"))), vars = "Close", class = c("distribution", "vctrs_vctr", "list"))
x[[1]]$mu
#> [1] 758.88