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.
Looking for a specific type of data to pull into R but don't know where to find it? You can try searching Quandl and Datamarket, where data can be downloaded in R format even without needing to install the site-specific packages mentioned above.
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
Reload it at any time with:
See the entire beginner's guide to R:
Sharon Machlis is online managing editor at Computerworld. Her e-mail address is email@example.com. You can follow her on Twitter @sharon000, on Facebook, on Google+ or by subscribing to her RSS feeds: articles; and blogs.
Read more about business intelligence/analytics in Computerworld's Business Intelligence/Analytics Topic Center.
This story, "Beginner's guide to R: Get your data into R" was originally published by Computerworld.