Getting strings recognized as variable names in R

后端 未结 6 1795
我在风中等你
我在风中等你 2020-11-28 18:39

Related: Strings as variable references in R
Possibly related: Concatenate expressions to subset a dataframe


I\'ve simplified the question per the comment

6条回答
  •  星月不相逢
    2020-11-28 19:06

    The basic answer to the question in the title is eval(as.symbol(variable_name_as_string)) as Josh O'Brien uses. e.g.

    var.name = "x"
    assign(var.name, 5)
    eval(as.symbol(var.name)) # outputs 5
    

    Or more simply:

    get(var.name) # 5
    

提交回复
热议问题