Convert a value of Paste to a variable

0

I have the following problem, I paste a character to form another character as follows:

paste0("SUMATOT", "VALORES", sep = "")

Now what I need is to convert that paste into a variable to which I can assign values, I used the get function but it does not work:

paste0("SUMATOT", "VALORES", sep = "", get) <- c(1, 2, 3)

Launches the following error

Error in paste0("SUMATOT", gsub(" ", "", DATA[1]), sep = "", get) <- SUM : 
target of assignment expands to non-language object

Thank you.

    
asked by Daniel Mendoza 04.12.2018 в 18:30
source

1 answer

2

If I understood correctly, what you are looking for is to name a variable with the result of a paste0() . You can do this using assign() that allows you to " assign "to the current environment the variable name indicated in the first parameter with the value of the second:

nombre_variable <- paste0("SUMATOT", "VALORES", sep = "")
assign(nombre_variable, c(1,2,3))

SUMATOTVALORES
[1] 1 2 3

Note: It is not mandatory to first define nombre_variable you can do it in one step.

If you do not want to initially assign the value and only define the memory space and the name of the variable, you should only indicate the least the type of value you intend to save:

assign(paste0("SUMATOT", "VALORES", sep = ""), numeric(0))
    
answered by 04.12.2018 / 18:37
source