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 ...

Get R Data Analysis Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.