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

Dynamic output

In the next two examples, sapply returns a matrix. If the function that it executes has row and column names defined, then sapply uses them for the matrix:

> sapply(auto[,2:6], summary) 
 
          mpg cylinders displacement horsepower weight 
Min.     9.00     3.000         68.0       46.0   1613 
1st Qu. 17.50     4.000        104.2       76.0   2224 
Median  23.00     4.000        148.5       92.0   2804 
Mean    23.51     5.455        193.4      104.1   2970 
3rd Qu. 29.00     8.000        262.0      125.0   3608 
Max.    46.60     8.000        455.0      230.0   5140 
 
> sapply(auto[,2:6], range) 
 
      mpg cylinders displacement horsepower weight 
[1,]  9.0         3           68         46   1613 
[2,] 46.6         8          455        230   5140 

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