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