-3

I am writing a for loop program in RStudio.

When I use the following command,

a <- 10
for (i in c(1:10335) { 
    a <- a + 0.005
    print(a)
}

I get a very large output in console, since the loop runs 10335 times. That large output is also a small fraction of the total output (a few, maybe 1000 or so, values from the last). I also loose my written program in the console. How do I fix this issue? How can I get a complete set of 10335 values in the output?

Also, is there any way to export this output in excel or in text format?

nrussell
  • 18,382
  • 4
  • 47
  • 60
  • what you want to have for output, your output is natural for the loop you have – Mateusz1981 Dec 14 '16 at 13:03
  • Are you not typing your code into the source file pane and executing it from there? – nrussell Dec 14 '16 at 13:04
  • What is your intention? Try `a <- 10`, `out <- 10 + c(0:9)*0.005` and have a look at `out`. Perhaps you also should have a look at `seq` – Christoph Dec 14 '16 at 13:07
  • 2
    You can save output to a file. You can store output in a vector (or matrix or dataframe) which can be exported (say as a csv file) which is then imported to Excel. If you store the output to a vector you can use the `View` function to inspect it. As you have discovered, having thousands of print statements is seldom helpful. – John Coleman Dec 14 '16 at 13:07
  • @nrussell No, I was trying to do it in console. Even with the source file pane, I am getting the same issue in console. –  Dec 14 '16 at 13:11

2 Answers2

2

Instead of printing the values, you can add them to an existing vector:

a <- 10
results <- vector(length = 10335)
for (i in c(1:10335)) {
a <- a + 0.005
results[i] <- a
}

str(results)
num [1:10335] 10 10 10 10 10 ...

You can save the result to a text file using write.table:

write.table(results, file = "results.txt", row.names = FALSE, col.names = FALSE)
Andrew J. Rech
  • 456
  • 4
  • 11
  • 5
    Don't do it this way -- preallocate space for the vector. You have just turned what should be a linear algorithm into a quadratic algorithm. – John Coleman Dec 14 '16 at 13:09
  • Isn't the whole vector copied on modification whether the length is preallocated or not? – Andrew J. Rech Dec 14 '16 at 13:20
  • 3
    No, it isn't. R would be a dreadfully slow language if things worked that way. See the accepted answer to : http://stackoverflow.com/q/12464379/4996248 about how to use `vector` to preallocate. Your answer is otherwise quite good. I'll upvote it if you tweak it (the downvote wasn't by me. by the way). – John Coleman Dec 14 '16 at 13:21
2

We can avoid for loop using seq():

# using seq instead of forloop
res <- seq(from = 10 + 0.005, to = 10 + 10335 * 0.005, by = 0.005)
# and write to a file
write.table(res, "temp_seq.txt", col.names = FALSE, row.names = FALSE)

Or if we must use loop then use sink() function:

# using loop and sink the output to a file
sink("temp_loop.txt")
a <- 10
for (i in c(1:10335)) { 
  a <- a + 0.005
  print(a)
}
sink()

In both cases we are writing output to a file, as RStudio console has a limits on print.

zx8754
  • 52,746
  • 12
  • 114
  • 209