Skip to content
Prev 293742 / 398513 Next

Saving a variable

Hi Trying,

Jim already suggested youuse write.table(), which I think is really
what you want.  I also wanted to point out that your outer loop is
unnecessary.  The following yields identical results and is *much*
faster.

randz <- matrix(rnorm(1000000), 500, 2000)
H <- matrix(0, 500, 2000)

H[1, ] <- randz[1, ]

for (i in 2:500){
  if(i < 251) {
    H[i, ] <- 0.6 * H[i-1, ] + randz[i, ]
  } else {
    H[i, ] <- H[i-1, ] + randz[i, ]
  }
}

write.table(H, file = "datad.txt")

There may be ways to optimize (or remove) the remaining loop, but at
least this first pass should move things along considerably.

Cheers,

Josh

On Sun, May 6, 2012 at 2:02 PM, Trying To learn again
<tryingtolearnagain at gmail.com> wrote: