O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

In this recipe, we cover data.table, which processes large amounts of data very efficiently, without our having to write detailed procedural code. To do this, follow these steps:

  1. Select columns from the dataset:
> autoDT[,.(mpg)] #selecting single column > autoDT[,.(mpg,horsepower,cylinders)] #selecting multiple column
  1. Filter all autoDT whose cylinders can either be in 3cyl or 4cyl :
> autoDT[cylinders %in% c("3cyl","4cyl")]>  #Filtering based on multiple condition> autoDT[cylinders=="3cyl" & horsepower>90] > autoDT[car_name %like% "chevrolet"] #Like operator for filtering
  1. Calculate the mean mpg for each cylinder type:
> autoDT[, mean(mpg), by=cylinders] cylinders V1 1: 4cyl 29.28676 2: 3cyl 20.55000 3: 6cyl 19.98571 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required