Avoiding the infamous “eval(parse())” construct

我只是一个虾纸丫 提交于 2019-11-27 08:03:47

Using get and [[:

bar <- list(foo = list(fast = 1:5, slow = 6:10),
            oof = list(6:10, 1:5))

rab <- 'bar'

get(rab)[['oof']]
# [[1]]
# [1]  6  7  8  9 10
# 
# [[2]]
# [1] 1 2 3 4 5

If the name of your top list is going to change and be accessed by a variable with the name then it is best to put those lists into another list, then you can access the list you want using [[. Also read fortune(312) and the help on ?'[['.

You can then access the pieces in a different ways (detailed on the help page ?'[[').

mylist <- list()
mylist$bar <- bar

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