Search code examples
rvectorvariable-names

Create a variable with a value that is the name of another variable in R


In R, how can you create a variable that it's value is the name of another variable?

For example: I have the vector groupers:

groupers <- c("Epinephelus.costae", "Epinephelus.marginatus",
              "Mycteroperca.rubra", "Serranus.cabrilla", "Serranus.scriba")

and I'd like to create an output that would be like that:

grp_name <- "groupers"

This seems like a simple operation but I don't know how the name of a vector is stored and therefore I can't figure out how to call it.

Cheers


Solution

  • If you want to take an object name and convert it to a character string, use this:

    grp_name <- deparse(substitute(groupers))
    grp_name
    # [1] "groupers"