Loops in R and adding a columns and values in a dataset
<p>I have no idea in R syntax, but I need to do simple computations with the data. I need to compute distances between objects (spatial)</p>

<pre><code>    str(cur_slice)
    Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   489 obs. of  24         variables:
     $ MMSI       : num  2.73e+08 2.11e+08 2.72e+08 2.73e+08 2.73e+08 ...
     $ LATITUDE   : num  45.4 44.1 46.9 44.7 45.2 ...
     $ LONGITUDE  : num  36.7 28.6 32 37.8 36.5 ...
    ....
</code></pre>

<p>I need to calculate the distances between each object listed in the cur_slice table.</p>

<p>Trying to do temporary array for coordinates using next loops.</p>

<pre><code>    &gt; myarray &lt;- NULL # Creates empty storage container
    &gt; for (row in seq(cur_slice)) {
        cur_MMSI&lt;- cur_slice$MMSI[raw]
        cur_lon&lt;-cur_slice$LONGITUDE[raw]
        cur_lat&lt;-cur_slice$LATITUDE[raw]
        for (raw2 in seq(cur_slice)) {
            if(!cur_slice$MMSI[raw2]==cur_MMSI) {
                myarray&lt;-c(myarray, cur_MMSI, cur_slice$MMSI[raw2], cur_lat, cur_lon, cur_slice$LATITUDE[raw2], cur_slice$LONGITUDE[raw2])
            }
        }
     }
</code></pre>

<p>But:</p>

<ol>
<li>I don't get the data in a table (myarray is simple list). How can I get a table, not a list?</li>
<li>my overall array is too big. E.g. in the sample cur_slice I have 489 objects... in overall array I have: 31962456 items... I understand that it's not rows, it's only items...</li>
</ol>

<p>How can I read data from one table and add it into another table (for future computation) and is there need to use for? </p>