r - How to make this loop faster? : <p>I am reading the <code>.csv</code> file named <code>cleanequityreturns.csv</code> which looks like this:</p>

<p><a href="https://i.stack.imgur.com/c2BR1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c2BR1.png" alt="enter image description here"></a></p>

<p>It goes from <code>r1</code> to <code>r299</code> and has 4,166 rows. The following code then creates a new file for each column, compute the approximate entropy using the <code>approx_entropy</code> function, and prints the value. I know creating a new file for each column is very tedious but I could not find another to do it.</p>

<pre><code>    equityreturn &lt;- read.csv("cleanequityreturns.csv", header=T)
for(i in 1:299) {
    file2 = paste(i, "equityret.csv", sep="")
    file5 = paste("r", i, sep="")
    file1 = subset(equityreturn, select=file5)
    write.table(file1, file2, sep="\t", row.names=FALSE, col.names=FALSE)
    file3 = paste("equity", i, sep="")
    file3 = matrix(scan(file = file2), nrow=4166, byrow=TRUE)
    print(approx_entropy(file3, edim = 4, r=0.441*sd(file3), elag = 1))
}
</code></pre>

<p>My problem is the following: it takes a long time for the code to perform these tasks. I tried running it for 10 columns and it took about 20 min, which translates in about 10h for all of the 299 columns. Also, this code prints each approximate entropy values, so I still have to copy and paste them in Excel to use them.</p>

<p>How could I make this code run faster and write the output in a <code>.csv</code> file?</p>
