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

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.

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:

install.packages('quantmod')

library('quantmod')

getSymbols("AAPL")

barChart(AAPL)

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:

barChart(AAPL['2013-04-01::2013-04-12'])

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:

rm(x)

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:

save.image()

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:

load("filename.rda")

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.

Join the CIO Australia group on LinkedIn. The group is open to CIOs, IT Directors, COOs, CTOs and senior IT managers.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags R Language

More about AdvancedAppleExcelGoogleMicrosoftSASSPSSTwitterUCLA

Show Comments
[]