I need to construct a set of variables where the variables should be constructed based on 2 parts: a) name b) a number that is stepped up with value [1]. For the increasing number i use a for-loop. I manage to create a string see test1, but not the increase the variable name, see test2.
Given the error code provided below I assume R does not want me to construct something using "paste0" that is part of the variable name.
My R-code:
numbers_for_variable_name <- c(1,2,3)
# Test-1 [works]
# Construct string with increasing number.
for (i in numbers_for_variable_name) {
cat(paste0("number-", i, "\n"))
}
# Test-2 [does not work]
# Construct variable name with increasing number.
for (i in numbers_for_variable_name) {
paste0("number-", i) <- "p1"
}
Outcome for "test1":
number-1
number-2
number-3
The error I get for test2 is:
Error in paste0("number-", i) <- "p1" :
target of assignment expands to non-language object
The expected outcome of "test2" is:
number-1 <- "p1"
number-2 <- "p1"
number-3 <- "p1"