Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a list of variables, like a1, a2, a3. Then I want to do a loop for these variable, for instance, assign values. I tried paste, which just gave character like "a1", that was not what I am really look for. Can anyone provide a solution?
We can use mget to get the values in a list
mget
list
lst <- mget(paste0("a", 1:3))
and then loop through the list and apply the function
lapply(lst, yourFunction)
Add a comment
The loop assigns values to all elements in your object. 'a1' gets value '2'; 'a2' gets value '3'; etcetera.
library(stringr) val <- str_c("a", 1:3) for (i in 1:length(val)){ assign(val[i], i+1) } get(val[1])#2 get(val[2])#3 get(val[3])#4
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.