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 ...
Get Mastering Data Analysis with R now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.