1

I want to assign variables a new value using a loop. Say I have N variables v1 through vN. They have some value stored already. I want to update the stored values by passing them to a function and store in the same variable. Something like:

v1 <- "value1"
v2 <- "value2"
.
.
.
vN <- "valueN"

v_vector <- c(v1,v2,v3,...,vN)

dummy_function <- function(x){paste("new",x)}

for (v in v_vector){
  v <- dummy_function(v) 
}

This should update the value of v1 to new value1, for example. How can I achieve this in R?

1 Answer 1

1

If we already created the vector objects, get them into a list and apply the function, and update the original objects with list2env

list2env(lapply(mget(ls(pattern = '^v\\d+$')), dummy_function), .GlobalEnv)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.