Converting polar coordinates to Cartesian coordinates I have a functional script that converts polar coordinates to Cartesian coordinates and then matches a value in a separate array to the coordinates. It works well, but I find that it takes a long time to run due to the length of the matrices being processed. Each file has four columns and 2,880,000 rows which means that I have 11,520,000 total values being processed. The data looks like this-

   [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3


The array rf.190301 is a three dimensional array that looks like this-

, , 1

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3

, , 2

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3

, , 3

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3


I'm fairly new to R and am just looking for a way to optimize what I'm trying to do in order to make it run a bit faster.

Polar2Cart &lt;- function(x) {

Cart.x &lt;- matrix(NA, nrow = 2880000, ncol = 4)

   for (i in 1:nrow(x)) {

      z[i]&lt;- x[i,1]
      t[i]&lt;- x[i,2]
      r[i]&lt;- x[i,3]

        theta.polar[i] &lt;- (x[i,2] * (pi/180))
        r.polar[i] &lt;- (x[i,3] * 0.075)

            x.cart[i] &lt;- r.polar[i]*cos(theta.polar[i])
            y.cart[i] &lt;- r.polar[i]*sin(theta.polar[i])
            value[i] &lt;- rf.190301[z[i], t[i], r[i]]

        Cart.x[i,] &lt;- cbind(z[i], y.cart[i], x.cart[i], value[i])
     }
}
You are missing a key feature of the R language: "vectorization". A fancy word for saying that most functions are implemented in such a way that you can provide them vectors and they will apply the computations to each element. As an example, the multiplication function * is vectorized so you can do r.polar &lt;- x[, 3] * 0.075 and it will return the full vector of x[i, 3] * 0.075 where i goes from 1 to nrow(x). This is less typing than doing for (i in 1:nrow(x)) { r.polar[i] &lt;- (x[i,3] * 0.075) } and also a lot faster as you can test.

Applying that idea throughout your code, you can rewrite your Polar2Cart as follows:

Polar2Cart &lt;- function(x) {
   z &lt;- x[, 1]
   t &lt;- x[, 2]
   r &lt;- x[, 3]
   theta.polar &lt;- t * pi / 180
   r.polar &lt;-  r * 0.075
   x.cart &lt;- r.polar * cos(theta.polar)
   y.cart &lt;- r.polar * sin(theta.polar)
   value  &lt;- rf.190301[cbind(z, t, r)]
   Cart.x &lt;- cbind(z, y.cart, x.cart, value)
}


One thing that needs explanation is the use of rf.190301[cbind(z, t, r)]. It is a little known use of the [ function documented as follows (you can access the doc by typing ?"[")


  A third form of indexing is via a numeric matrix with one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector.


Finally, a recommendation. Be careful using single characters as variable names, since R already uses a few of them for built-in variables or functions. You could for example confuse your variable t for the t() function for transposing data. Other single character variable names used by R that come to mind are c (concatenation), q (quit), T (TRUE), F (FALSE), I (inhibit). Anyway, it is always better to use descriptive variable names and that usually implies more than one character.