Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I try to create a set of values with a for loop using paste0 but get an error:

for(i in 1:3){
  paste0('value_',i) <- paste0('test',i)
}

Fehler in paste0("value_", i) <- i : 
  Ziel der Zuweisung expandiert zu keinem Sprachobjekt

I want it like this but with a for loop:

value_1 <- paste0('test',1)
value_2 <- paste0('test',2)
value_3 <- paste0('test',3)

> value_1
[1] "test1"
> value_2
[1] "test2"
> value_3
[1] "test3"

How can I solve that?

question from:https://stackoverflow.com/questions/66050214/how-can-i-set-a-value-using-paste0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
222 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can try createing a names list, and then write the list's elements to the global environment

L <- lapply( 1:3, function(x) paste0( "test", x ) )
names(L) <- paste0( "value_", 1:3 )
list2env( L, envir = .GlobalEnv ) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...