-4

I want to make

a1 <- 1
a2 <- 2
a3 <- 3

I want to use

for (i in 1:3) {
paste("a",i) <- i
}

but this clearly doesn't work. Is there any way to this kind of stuff easily?

kim1298
  • 31
  • 3
  • 9

1 Answers1

4

We may need assign here

for (i in 1:3) {
  assign(paste0("a",i), value =i)
}

NOTE: It is not recommended to create multiple objects in the global environment, instead we can keep them in a list or even a named vector (in this case)

lst <- setNames(as.list(1:3), paste0("a", 1:3))
akrun
  • 874,273
  • 37
  • 540
  • 662