Book Image

Mastering Data analysis with R

By : Gergely Daróczi
Book Image

Mastering Data analysis with R

By: Gergely Daróczi

Overview of this book

Table of Contents (19 chapters)
Mastering Data Analysis with R
Credits
www.PacktPub.com
Preface

Computing new variables


One of the most trivial actions we usually perform while restructuring a dataset is to create a new variable. For a traditional data.frame, it's as simple as assigning a vector to a new variable of the R object.

Well, this method also works with data.table, but the usage is deprecated due to the fact that there is a much more efficient way of creating one, or even multiple columns in the dataset:

> hflights_dt <- data.table(hflights)
> hflights_dt[, DistanceKMs := Distance / 0.62137]

We have just computed the distances, in kilometers, between the origin and destination airports with a simple division; although all the hardcore users can head for the udunits2 package, which includes a bunch of conversion tools based on Unidata's udunits library.

And as can be seen previously, data.table uses that special := assignment operator inside of the square brackets, which might seem strange at first glance, but you will love it!

Note

The := operator can be more than 500...