0

I am attempting to create a looping block of code in an R script that calls different row numbers each time through. I had thought to create a value n that increases by 1 each loop.

n <- 1
paste("choice", n, sep = "") <- readline(prompt = paste("Enter reference file for ", filenames[n], " and ", filenames[n+1], ": ))
some.function
some.function
some.function
n <- n+1

And so on. My hangup currently is that I can't seem to figure out how to get the user input to output to the paste function. When I try this current format I receive the error "target of assignment expands to non-language object". Is there any way for me to do this or am I barking up the wrong tree?

  • 2
    Do you really need to create a bunch of variables with indexes in their name? That's very difficult to work with in R (tough technically you can with `assign()`). It would be better to just save the values to a list or some sort. Your example isn't complete enough to show you exactly how to do that. It would be better if you included a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 30 '20 at 22:34
  • See also https://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f – MrFlick Jun 30 '20 at 22:36
  • Yes you're totally right. This worked but I realized you're correct, I don't need a bunch of numerically distinct variables. Thanks for the help! – Wesley Hanson Jul 01 '20 at 14:44

0 Answers0