In R , when I use “print”, I can see all the values, but how can I save this as a vector?
For example, in a ‘for’ loop:
for(i in 1:10), I would like the value of A , when i= 1,2,3,4….. but if I use x=A, it only saves the final value of A which is the value when i = 10. So, how can I save the values in print(A)?
Additionally, I use more than one ‘for’ loop e.g.:
for (i in 1:9) {
for (k in 1:4) {
}
}
Consequently, x[i]=A..does not work very well here.
I think Etiennebr’s answer shows you what you should do, but here is how to capture the output of
printas you say you want: use thecapture.outputfunction.You can see that it captures everything exactly as printed. To avoid having all the
[1]s, you can usecatinstead of print:Once again, you probably don’t really want to do this for your application, but there are situations when this approach is useful (eg. to hide automatically printed text that some functions insist on).
EDIT:
Since the output of
printorcatis a string, if you capture it, it still will be a string and will have quotation marks To remove the quotes, just useas.numeric: