Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
R Data Analysis Cookbook - Second Edition

You're reading from  R Data Analysis Cookbook - Second Edition

Product type Book
Published in Sep 2017
Publisher Packt
ISBN-13 9781787124479
Pages 560 pages
Edition 2nd Edition
Languages
Authors (3):
Kuntal Ganguly Kuntal Ganguly
Profile icon Kuntal Ganguly
Shanthi Viswanathan Shanthi Viswanathan
Profile icon Shanthi Viswanathan
Viswa Viswanathan Viswa Viswanathan
Profile icon Viswa Viswanathan
View More author details
Toc

Table of Contents (14) Chapters close

Preface 1. Acquire and Prepare the Ingredients - Your Data 2. What's in There - Exploratory Data Analysis 3. Where Does It Belong? Classification 4. Give Me a Number - Regression 5. Can you Simplify That? Data Reduction Techniques 6. Lessons from History - Time Series Analysis 7. How does it look? - Advanced data visualization 8. This may also interest you - Building Recommendations 9. It's All About Your Connections - Social Network Analysis 10. Put Your Best Foot Forward - Document and Present Your Analysis 11. Work Smarter, Not Harder - Efficient and Elegant R Code 12. Where in the World? Geospatial Analysis 13. Playing Nice - Connecting to Other Systems

Rescaling a variable to specified min-max range

Distance computations play a big role in many data analytics techniques. We know that variables with higher values tend to dominate distance computations and you may want to rescale the values to be in the range of 0 - 1.

Getting ready

Install the scales package and read the data-conversion.csv file from the book's data for this chapter into your R environment's working directory:

> install.packages("scales")
> library(scales)
> students <- read.csv("data-conversion.csv")

How to do it...

To rescale the Income variable to the range [0,1], use the following code snippet:

> students$Income.rescaled <- rescale(students$Income) 

How it works...

By default, the rescale() function makes the lowest value(s) zero and the highest value(s) one. It rescales all the other values proportionately. The following two expressions provide identical results:

> rescale(students$Income) 
> (students$Income - min(students$Income)) / (max(students$Income) - min(students$Income))

To rescale a different range than [0,1], use the to argument. The following snippet rescales students$Income to the range (0,100):

> rescale(students$Income, to = c(1, 100)) 

There's more...

When using distance-based techniques, you may need to rescale several variables. You may find it tedious to scale one variable at a time.

Rescaling many variables at once

Use the following function to rescale variables:

rescale.many <- function(dat, column.nos) { 
nms <- names(dat)
for(col in column.nos) {
name <- paste(nms[col],".rescaled", sep = "")
dat[name] <- rescale(dat[,col])
}
cat(paste("Rescaled ", length(column.nos), " variable(s)n"))
dat
}

With the preceding function defined, we can do the following to rescale the first and fourth variables in the data frame:

> rescale.many(students, c(1,4)) 

See also

  • The Normalizing or standardizing data in a data frame recipe in this chapter.
You have been reading a chapter from
R Data Analysis Cookbook - Second Edition
Published in: Sep 2017 Publisher: Packt ISBN-13: 9781787124479
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 $15.99/month. Cancel anytime