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

Adding multiple aggregated columns

In step 4, we added one calculated column meanmpg. The := syntax computes the variable and merges it into the original data:

> # calculate median and sd of mpg grouped by cylinders 
> autoDT[,c("medianmpg","sdmpg") := list(median(mpg),sd(mpg)),     by=cylinders] 
> # Display selected columns of autoDT table for the first 5 rows 
> autoDT[1:5,c(3,9:12), with=FALSE] 
   cylinders            car_name  meanmpg medianmpg    sdmpg 
1:      3cyl     mazda rx2 coupe 20.55000     20.25 2.564501 
2:      3cyl           maxda rx3 20.55000     20.25 2.564501 
3:      3cyl       mazda rx-7 gs 20.55000     20.25 2.564501 
4:      3cyl          mazda rx-4 20.55000     20.25 2.564501 
5:      4cyl chevrolet vega 2300 29.28676     28.25 5.710156 

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