How to name variables on the fly?

前端 未结 6 1585
执念已碎
执念已碎 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:16

    It seems to me that you might be better off with a list rather than using orca1, orca2, etc, ... then it would be orca[1], orca[2], ...

    Usually you're making a list of variables differentiated by nothing but a number because that number would be a convenient way to access them later.

    orca <- list()
    orca[1] <- "Hi"
    orca[2] <- 59
    

    Otherwise, assign is just what you want.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 10:24

    And this option?

    list_name<-list()
    for(i in 1:100){
        paste("orca",i,sep="")->list_name[[i]]
    }
    

    It works perfectly. In the example you put, first line is missing, and then gives you the error message.

    0 讨论(0)
  • 2020-11-22 10:28

    Don't make data frames. Keep the list, name its elements but do not attach it.

    The biggest reason for this is that if you make variables on the go, almost always you will later on have to iterate through each one of them to perform something useful. There you will again be forced to iterate through each one of the names that you have created on the fly.

    It is far easier to name the elements of the list and iterate through the names.

    As far as attach is concerned, its really bad programming practice in R and can lead to a lot of trouble if you are not careful.

    0 讨论(0)
  • Use assign:

    assign(paste("orca", i, sep = ""), list_name[[i]])
    
    0 讨论(0)
  • 2020-11-22 10:31

    Another tricky solution is to name elements of list and attach it:

    list_name = list(
        head(iris),
        head(swiss),
        head(airquality)
        )
    
    names(list_name) <- paste("orca", seq_along(list_name), sep="")
    attach(list_name)
    
    orca1
    #   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    # 1          5.1         3.5          1.4         0.2  setosa
    # 2          4.9         3.0          1.4         0.2  setosa
    # 3          4.7         3.2          1.3         0.2  setosa
    # 4          4.6         3.1          1.5         0.2  setosa
    # 5          5.0         3.6          1.4         0.2  setosa
    # 6          5.4         3.9          1.7         0.4  setosa
    
    0 讨论(0)
提交回复
热议问题