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

后端 未结 4 1994
没有蜡笔的小新
没有蜡笔的小新 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:30

    Very bad idea; you should never use eval or parse in R, unless you perfectly know what you are doing.
    Variables can be created using:

    name<-"x"
    assign(name,3) #Eqiv to x<-3
    

    And removed by:

    name<-"x"
    rm(list=name)
    

    But in your case, it can be done with simple named vector:

    apply(x,3,mean)->v;names(v)<-letters[1:length(v)]
    v
    v["b"]
    #Some operations on v
    rm(v)
    

提交回复
热议问题