Beginner's guide to R: Get your data into R

In part 2 of our hands-on guide to the hot data-analysis environment, we provide some tips on how to import data in various formats, both local and on the Web.

1 2 3 4 Page 4
Page 4 of 4

Help with external data

R enthusiasts have created add-on packages to help other users download data into R with a minimum of fuss.

For instance, the financial analysis package Quantmod, developed by quantitative software analyst Jeffrey Ryan, makes it easy to not only pull in and analyze stock prices but graph them as well.

All you need are four short lines of code to install the Quantmod package, load it, retrieve a company's stock prices and then chart them using the barChart function. Type in and run the following in your R editor window or console for Apple data:





Want to see just the last couple of weeks? You can use a command like this:

barChart(AAPL, subset='last 14 days')

chartSeries(AAPL, subset='last 14 days')

Or grab a particular date range like this:


Quantmod is a very powerful financial analysis package, and you can read more about it on the Quantmod website.

There are many other packages with R interfaces to data sources such as twitteR for analyzing Twitter data; Quandl and rdatamarket for access to millions of data sets at Quandl and Data Market, respectively; and several for Google Analytics, including rga, RGoogleAnalytics and ganalytics.

Removing unneeded data

If you're finished with variable x and want to remove it from your workspace, use the rm() remove function:


Saving your data

Once you've read in your data and set up your objects just the way you want them, you can save your work in several ways. It's a good idea to store your commands in a script file, so you can repeat your work if needed.

How best to save your commands? You can type them first into the RStudio script editor (top left window) instead of directly into the interactive console, so you can save the script file when you're finished. If you haven't been doing that, you can find a history of all the commands you've typed in the history tab in the top right window; select the ones you want and click the "to source" menu option to copy them into a file in the script window for saving.

You can also save your entire workspace. While you're in R, use the function:


That stores your workspace to a file named .RData by default. This will ensure you don't lose all your work in the event of a power glitch or system reboot while you've stepped away.

When you close R, it asks if you want to save your workspace. If you say yes, the next time you start R that workspace will be loaded. That saved file will be named .RData as well. If you have different projects in different directories, each can have its own .RData workspace file.

You can also save an individual R object for later loading with the save function:

save(variablename, file="filename.rda")

Reload it at any time with:


For even speedier saving and loading, check out the feather and fst packages. feather can also read/write to Python, making it a great choice for people or teams who use both languages. fst, in addition to being very fast, also offers compression.

Next: Easy ways to do basic data analysis in R.

Ready to do more with R? Download the free PDF Advanced Beginner's Guide to R.

Copyright © 2017 IDG Communications, Inc.

1 2 3 4 Page 4
Page 4 of 4
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon