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:
- Select columns from the dataset:
> autoDT[,.(mpg)] #selecting single column > autoDT[,.(mpg,horsepower,cylinders)] #selecting multiple column
- 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
- 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 ...