Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
R Data Visualization Cookbook

You're reading from   R Data Visualization Cookbook Over 80 recipes to analyze data and create stunning visualizations with R

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783989508
Length 236 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. A Simple Guide to R FREE CHAPTER 2. Basic and Interactive Plots 3. Heat Maps and Dendrograms 4. Maps 5. The Pie Chart and Its Alternatives 6. Adding the Third Dimension 7. Data in Higher Dimensions 8. Visualizing Continuous Data 9. Visualizing Text and XKCD-style Plots 10. Creating Applications in R Index

Importing data in R

Data comes in various formats. Most of the data available online can be downloaded in the form of text documents (.txt extension) or as comma-separated values (.csv). We also encounter data in the tab-delimited format, XLS, HTML, JSON, XML, and so on. If you are interested in working with data, either in JSON or XML, refer to the recipe Constructing a bar plot using XML in R in Chapter 10, Creating Applications in R.

How to do it...

In order to import a CSV file in R, we can use the read.csv() function:

test = read.csv("raw.csv", sep = ",", header = TRUE)

Alternatively, read.table() function allows us to import data with different separators and formats. Following are some of the methods used to import data in R:

How to do it...

How it works…

The first argument in the read.csv() function is the filename, followed by the separator used in the file. The header = TRUE argument is used to instruct R that the file contains headers. Please note that R will search for this file in its current directory. We have to specify the directory containing the file using the setwd() function. Alternatively, we can navigate and set our working directory by navigating to Sessions | Set working directory | Choose directory.

The first argument in the read.table() function is the filename that contains the data, the second argument states that the data contains the header, and the third argument is related to the separator. If our data consists of a semi colon (;), a tab delimited, or the @ symbol as a separator, we can specify this under the sep ="" argument. Note that, to specify a separator as a tab delimited, users would have to substitute sep = "," with sep ="\t" in the read.table() function.

One of the other useful arguments is the row.names argument. If we omit row.names, R will use the column serial numbers as row.names. We can assign row.names for our data by specifying it as row.names = c("Name").

You have been reading a chapter from
R Data Visualization Cookbook
Published in: Jan 2015
Publisher:
ISBN-13: 9781783989508
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image