How to name variables on the fly?

前端 未结 6 1680
执念已碎
执念已碎 2020-11-22 10:11

Is it possible to create new variable names on the fly?

I\'d like to read data frames from a list into new variables with numbers at the end. Something like orca1, o

6条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 10:21

    FAQ says:

    If you have

    varname <- c("a", "b", "d")
    

    you can do

    get(varname[1]) + 2
    

    for

    a + 2
    

    or

    assign(varname[1], 2 + 2)
    

    for

    a <- 2 + 2
    

    So it looks like you use GET when you want to evaluate a formula that uses a variable (such as a concatenate), and ASSIGN when you want to assign a value to a pre-declared variable.

    Syntax for assign: assign(x, value)

    x: a variable name, given as a character string. No coercion is done, and the first element of a character vector of length greater than one will be used, with a warning.

    value: value to be assigned to x.

提交回复
热议问题