<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/load-save-and-rda-files/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://thepracticalr.wordpress.com/2017/04/27/load-save-and-rda-files/"> The Practical R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A couple weeks ago I stumbled across a feature in R that I had never heard of before. The functions <strong>save()</strong>, <strong>load()</strong>, and the R file type .rda.</p>
<p>The .rda files allow a user to save their R data structures such as vectors, matrices, and data frames. The file is automatically compressed, with user options for additional compression. Let’s take a look.</p>
<p>First, we will grab one of the built-in R datasets. We can view these by calling <strong>data()</strong>. Let’s use the “Orange” dataset.</p>
<pre><code># get the Orange data
Orange
   Tree  age circumference
1     1  118            30
2     1  484            58
3     1  664            87
4     1 1004           115
5     1 1231           120
6     1 1372           142
7     1 1582           145
8     2  118            33
9     2  484            69
10    2  664           111
11    2 1004           156
12    2 1231           172
13    2 1372           203
14    2 1582           203
15    3  118            30
16    3  484            51
17    3  664            75
18    3 1004           108
19    3 1231           115
20    3 1372           139
21    3 1582           140
22    4  118            32
23    4  484            62
24    4  664           112
25    4 1004           167
26    4 1231           179
27    4 1372           209
28    4 1582           214
29    5  118            30
30    5  484            49
31    5  664            81
32    5 1004           125
33    5 1231           142
34    5 1372           174
35    5 1582           177
</code></pre>
<p>Next, let’s save each column individually as vectors.</p>
<pre><code># save the Orange data as vectors
count&lt;-Orange$Tree
age&lt;-Orange$age
circumference&lt;-Orange$circumference
</code></pre>
<p>Now if we look at our variables in the RStudio environment, we can see count, age, and circumference saved there.<br>
<img sizes="(max-width: 696px) 100vw, 696px" data-image-description="" data-orig-file="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456" data-orig-size="696,158" data-image-title="saved_files" class="wp-image-830" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="830" data-permalink="https://thepracticalr.wordpress.com/2017/04/27/load-save-and-rda-files/saved_files/#main" alt="saved_files" data-medium-file="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456?w=300" data-large-file="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456?w=696" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456"></p>
<p>Next, let’s set our R working directory, so the .rda file will save in the correct location. First we’ll use <strong>getwd()</strong> to find our current working directory, then we’ll adjust it (if needed) using <strong>setwd()</strong>. I set my working directory to a folder on the D drive.</p>
<pre><code>#get and set working directory
getwd()
[1] &quot;D:/Users&quot;
setwd(&quot;D:/r-temp&quot;)
> getwd()
[1] &quot;D:/r-temp&quot;
</code></pre>
<p>Finally, let’s use the <strong>save()</strong> command to save our 3 vectors to an .rda file. The “file” name will be the name of the new .rda file.</p>
<pre><code>#save to rda file
save(count, age, circumference, file = &quot;mydata.rda&quot;)
</code></pre>
<p>Next we will remove our R environment variables using the command <strong>rm()</strong>.</p>
<pre><code>#remove variables
rm(age, circumference, count)
</code></pre>
<p>Now we can see that we no longer have saved variables in our R workspace.<br>
<img sizes="(max-width: 696px) 100vw, 696px" data-image-description="" data-orig-file="https://thepracticalr.files.wordpress.com/2017/04/no-saved-files.png?w=456" data-orig-size="696,167" data-image-title="no-saved-files" data-comments-opened="1" class="wp-image-853" data-recalc-dims="1" data-attachment-id="853" data-permalink="https://thepracticalr.wordpress.com/2017/04/27/load-save-and-rda-files/no-saved-files/#main" alt="no-saved-files" data-medium-file="https://thepracticalr.files.wordpress.com/2017/04/no-saved-files.png?w=456?w=300" data-large-file="https://thepracticalr.files.wordpress.com/2017/04/no-saved-files.png?w=456?w=696" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://thepracticalr.files.wordpress.com/2017/04/no-saved-files.png?w=456"></p>
<p>Now, we can check that our .rda file (myrda.rda) does in fact store our data by using the <strong>load()</strong> command.<br>
Note: If we had not properly set our working directory, then we would have needed to provide a full path to the rda file. For example, “C:/Users/Documents/R files/myrda” rather than just “myrda”.</p>
<pre><code>#load the rda file
load(file = &quot;mydata.rda&quot;)
</code></pre>
<p>Great, now we can see that our variables are back in the R environment for use once more.</p>
<p><img sizes="(max-width: 696px) 100vw, 696px" data-image-description="" data-orig-file="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456" data-orig-size="696,158" data-image-title="saved_files" data-comments-opened="1" class="wp-image-830" data-recalc-dims="1" data-attachment-id="830" data-permalink="https://thepracticalr.wordpress.com/2017/04/27/load-save-and-rda-files/saved_files/#main" alt="saved_files" data-medium-file="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456?w=300" data-large-file="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456?w=696" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://thepracticalr.files.wordpress.com/2017/04/saved_files.png?w=456"></p>
<p>Saving and loading data in R might be very useful when you’re working with large datasets that you want to clear from your memory, but you also would like to save for later. It also might be useful for long, complex R workflows and scripts. You can control the compression of the file using the settings ‘compress’ and ‘compression_level’.</p>
<p>That’s all for now!</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thepracticalr.wordpress.com/809/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/thepracticalr.wordpress.com/809/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://thepracticalr.wordpress.com/2017/04/27/load-save-and-rda-files/"> The Practical R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ZQ0B3Yoa9F0"></a></div>