Assigning and removing objects in a loop: eval(parse(paste(

后端 未结 4 1995
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 09:07

I am looking to assign objects in a loop. I\'ve read that some form of eval(parse( is what I need to perform this, but I\'m running into errors listing in

4条回答
  •  盖世英雄少女心
    2020-12-16 09:28

    That's a pretty disgusting and frustrating way to go about it. Use assign to assign and rm's list argument to remove objects.

    > for (i in 1:length(x[1,1,])) {
    +   assign(letters[i],mean(x[,,i]))
    + }
    > ls()
    [1] "a" "b" "c" "i" "x"
    > a
    [1] 3.5
    > b
    [1] 9.5
    > c
    [1] 15.5
    > for (i in 1:length(x[1,1,])) {
    +   rm(list=letters[i])
    + }
    > ls()
    [1] "i" "x"
    > 
    

    Whenever you feel the need to use parse, remember fortune(106):

    If the answer is parse() you should usually rethink the question.
    -- Thomas Lumley, R-help (February 2005)

提交回复
热议问题