Problems with accessing double elements in a list in R

寵の児 提交于 2020-11-29 19:20:35

问题


I have performed a bootstrapping with 2.000 resamples of the Lee Carter model for mortality projection. The question is not specific for mortality studies, but on more general dimensions in R.

After performing the bootstrapping I get a list with 2000 elements, each for every of 2.000 re-estimations of the model. For each model, there are estimates of my 3 variables: a_x, b_x and k_t. Both a_x and b_x are age-specific, so the "x" denotes an age in the interval [0:95].

I would now like to plot a histogram of all the b_x values for age x = 70.

### Performing the bootstrap:
JA_lc_fitM_boot1 <- bootstrap(LCfit_JA_M, nBoot = 2000, type = "semiparametric")

### Plotting the histogram with all b_x for x = 70:
JA_lc_fitM_boot1[["bootParameters"]][1:2000][["bx"]][[70]]

I have tried multiple options, but I cannot make it work. The thing that triggers me, is that I am working with a double within a list of a list.

I have added a picture of the data below:

Does anybody have a solution to this?


回答1:


It looks like you need the apply family of functions. Your data is not reproducible, so I can't confirm this will work, but if you do:

result <- sapply(JA_lc_fitM_boot1[["bootParameters"]], function(var) var[["bx"]][[70]])

You should get what you're looking for.




回答2:


You may want to have a look at the purrr package and the family of map functions, or tidyr and the hoist funtion.

(If you want code that works, you indeed need to provide some data!)



来源:https://stackoverflow.com/questions/61078813/problems-with-accessing-double-elements-in-a-list-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!