0

I know how to generate variable and assign value to it from here: Create a variable name with "paste" in R?

But I do not need to assign value to generated variable, I need to use variable which name is generated. Here is my code:

a1 <- 1
a2 <- 2
a3 <- 3
a4 <- 4
a5 <- 5

counter <- 1
while(counter <= 5)
{
  (paste0("a", counter) + 1)
  counter <- counter +1
}

Question: How to modify (paste0("a", counter) + 1) so that code above will output in console:

2
3
4
5
6
vasili111
  • 6,032
  • 10
  • 50
  • 80
  • Should this be `sapply(mget(paste0("a", 1:5)), `+`, 1)` – akrun Nov 20 '19 at 21:47
  • @akrun You mean `sapply(mget(paste0("a", 1:5)), +, 1)`? If yes, it gives me error: `Error in source("~/.active-rstudio-document", echo = TRUE) : ~/.active-rstudio-document:10:35: unexpected ',' 9: { 10: sapply(mget(paste0("a", 1:5)), +,` – vasili111 Nov 20 '19 at 21:50
  • sorry, i meant backquotes on `+` – akrun Nov 20 '19 at 21:51
  • You really should avoid code like this in R. Having indexes embedded in variable names makes these values much harder to work with. How did you create these variables in the first place? You should rethink your strategy. – MrFlick Nov 20 '19 at 22:03
  • @MrFlick The problem is that I have lots of variables like `a1, a2, ....`. I need them to use them in loop one by one. Do you know any better way? – vasili111 Nov 20 '19 at 22:07
  • Absolutely there is a better way. Store related values in a list. It’s unclear how you wound up with those variables in the first place. This is really an XY problem because we are attempting to fix your “solution” rather than whatever the real problem is. – MrFlick Nov 20 '19 at 22:09
  • @MrFlick I understand your concern regarding quality of my question and you are right. I was given that kind of data and I need to work with it (this is from where that kind of variables are comming from). – vasili111 Nov 20 '19 at 22:12

2 Answers2

3

You do that by doing it differently and storing the variables in a list:

a <- setNames(as.list(1:5),paste0("a",1:5))

counter <- 1
while(counter <= 5)
{
  idx <- paste0("a",counter)
  print(a[[idx]] + 1)
  counter <- counter +1
}
joran
  • 169,992
  • 32
  • 429
  • 468
1

Another solution: in the loop, you assign a new value to each a* which is their previous value + 1. Then you obtain the value of each a* and you print it. Finally, counter increases of one unit.

a1 <- 1
a2 <- 2
a3 <- 3
a4 <- 4
a5 <- 5

counter <- 1
while(counter <= 5)
{
  assign(paste0("a", counter), counter + 1)
  print(get(paste0("a", counter)))
  counter <- counter + 1
}
bretauv
  • 7,756
  • 2
  • 20
  • 57
  • I do not understand why it was down voted. It does exactly what I need way I need. – vasili111 Nov 20 '19 at 22:23
  • I didn't know this, thanks for the remark but why is this bad practice? – bretauv Nov 20 '19 at 22:39
  • You mean the discussion in question comments? I think @MrFlick means that data should not have that kind of variable naming and that should be corrected in first place. – vasili111 Nov 20 '19 at 22:45
  • No, @joran had put a comment saying that using ```assign``` and ```get``` was bad practice so I wanted to know why but apparently he/she removed his/her comment – bretauv Nov 20 '19 at 22:49
  • Maybe you will also be interested to look at: https://stackoverflow.com/questions/58966113/how-to-generate-data-frame-variable-name-on-the-fly-and-address-existing-data-fr – vasili111 Nov 21 '19 at 03:39