Create a variable name with "paste" in R?
See below:
paste("perf.a", "1", sep="")
# [1] "perf.a1"
What if I want to assign a value to perf.a1
?
I tried as.name
, as.symbol
, etc., with no avail:
as.name(paste("perf.a", "1", sep="")) = 5
# Error in as.name(paste("perf.a", "1", sep = "")) = 5 :
# target of assignment expands to non-language object
as.symbol(paste("perf.a", "1", sep="")) = 5
# Error in as.symbol(paste("perf.a", "1", sep = "")) = 5 :
# target of assignment expands to non-language object
noquote(paste("perf.a", "1", sep="")) = 5
# Error in noquote(paste("perf.a", "1", sep = "")) = 5 :
# target of assignment expands to non-language object
Solution 1:
You can use assign
(doc) to change the value of perf.a1:
> assign(paste("perf.a", "1", sep=""),5)
> perf.a1
[1] 5
Solution 2:
See ?assign
.
> assign(paste("tra.", 1, sep = ""), 5)
> tra.1
[1] 5