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

Special values in R

R comes with some special values. Some of the special values in R are NA, Inf, -Inf, and NaN.

Special values in R

How to do it…

The missing values are represented in R by NA. When we download data, it may have missing data and this is represented in R by NA:

z = c( 1,2,3, NA,5,NA) # NA in R is missing Data

To detect missing values, we can use the install.packages() function or is.na(), as shown:

complete.cases(z) # function to detect NA
is.na(z) # function to detect NA

To remove the NA values from our data, we can type the following in our active R session console window:

clean <- complete.cases(z)
z[clean] # used to remove NA from data

Please note the use of square brackets ([ ]) instead of parentheses.

In R, not a number is abbreviated as NaN. The following lines will generate NaN values:

##NaN
0/0
m <- c(2/3,3/3,0/0)
m

The is.finite, is.infinite, or is.nan functions will generate logical values (TRUE or FALSE).

is.finite(m)
is.infinite(m)
is.nan(m)

The following line will generate inf as a special value in R:

## infinite
k = 1/0

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How it works…

complete.cases(z) is a logical vector indicating complete cases that have no missing value (NA). On the other hand, is.na(z) indicates which elements are missing. In both cases, the argument is our data, a vector, or a matrix.

How it works…

R also allows its users to check if any element in a matrix or a vector is NA by using the anyNA() function. We can coerce or assign NA to any element of a vector using the square brackets ([ ]). The [3] input instructs R to assign NA to the third element of the dk vector.

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