Skip to content

Instantly share code, notes, and snippets.

@sibyvt
Last active May 8, 2016 04:24
Show Gist options
  • Save sibyvt/afa28569848ffb55eff40d8fa4821977 to your computer and use it in GitHub Desktop.
Save sibyvt/afa28569848ffb55eff40d8fa4821977 to your computer and use it in GitHub Desktop.
> library(datasets)
> data(mtcars)
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> ?mtcars
> summary(mtcars)
mpg cyl disp hp
Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
Median :19.20 Median :6.000 Median :196.3 Median :123.0
Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
drat wt qsec vs
Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
Median :3.695 Median :3.325 Median :17.71 Median :0.0000
Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
am gear carb
Min. :0.0000 Min. :3.000 Min. :1.000
1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
Median :0.0000 Median :4.000 Median :2.000
Mean :0.4062 Mean :3.688 Mean :2.812
3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
Max. :1.0000 Max. :5.000 Max. :8.000
> x <- matrix(rnorm(352), 32, 11)
> apply(x,1,mean)
[1] -0.30143181 -0.15236132 0.45883926 0.11672328 -0.30815410 0.07105500
[7] -0.14791146 -0.19558925 0.37633710 0.42164804 0.08457998 -0.03985195
[13] -0.31569101 0.02186848 -0.39662810 0.12294262 -0.18668923 -0.19568227
[19] -0.28842253 0.29148884 0.08075201 -0.16104420 -0.21106474 -0.22896730
[25] -0.15781185 0.31265298 -0.64001496 -0.30848061 0.22978252 0.13095760
[31] 0.01152811 0.44386672
> apply(mtcars, 2, mean)
mpg cyl disp hp drat wt qsec
20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
vs am gear carb
0.437500 0.406250 3.687500 2.812500
> tapply(mtcars$mpg, mtcars$cyl, mean)
4 6 8
26.66364 19.74286 15.10000
> tapply(mtcars$hp, mtcars$cyl, mean)
4 6 8
82.63636 122.28571 209.21429
> data(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> x <- matrix(rnorm(750), 150, 5)
> apply(x,1,mean)
[1] 0.659387698 0.431731860 0.134566944 -0.189169333 0.194876124
[6] 0.880537038 -0.871515871 -0.296238805 0.030139431 0.377331394
[11] -0.735135463 0.027529597 0.274933609 0.158953010 -0.123462350
[16] -0.422585255 0.328762801 0.396664601 0.729689214 0.172078383
[21] 0.058934330 0.616034007 -0.056157564 -0.338974790 -0.801289908
[26] -0.500203953 -0.380118923 0.078170700 0.335604031 -0.414949871
[31] 0.245320129 0.018508156 0.078367765 -0.151087871 -0.883318138
[36] -0.350814244 -0.440460626 -0.058596714 0.420784146 -0.313211658
[41] -0.259304865 -0.127707105 0.462488702 0.034952835 0.529501711
[46] -0.528424177 -0.283491315 0.475358502 -0.286423532 0.318280993
[51] 0.612352556 0.097170006 0.316715233 -0.341018337 0.118983702
[56] -0.534898281 -0.325978637 0.582299452 -0.594499389 -0.274230487
[61] 0.329452697 -0.618480232 -0.653095787 0.529996383 0.276090231
[66] -0.056896218 -0.197979558 0.005495474 -0.871490367 0.521324232
[71] -0.072085433 0.126642772 0.224640771 0.361575396 -0.913408163
[76] -0.204381686 -0.290780112 -0.438781412 -0.367274920 -0.393090909
[81] -0.335248632 -0.104692780 -0.162249250 -0.582973404 -0.418817111
[86] 0.276867813 -0.938181111 0.138681945 -0.215487302 0.811490263
[91] 0.060699050 -0.577839758 -0.112169037 -0.020605406 -0.174738488
[96] -0.581888158 -0.281843044 -0.415132070 -0.488300154 -0.276114402
[101] 0.139524227 0.642782846 0.033891593 -0.581783999 -0.089440486
[106] 0.029351132 0.310578168 0.185419988 0.520155027 -0.042835389
[111] 0.481857371 -0.002670153 -0.060409958 -0.716541686 -0.179149379
[116] 0.446914092 -0.367886059 -0.243514051 0.936486431 -0.189549581
[121] 0.498096696 0.068948777 0.252417161 0.696250324 0.044326030
[126] -0.802097460 0.327962117 0.031417215 -0.258312148 -0.201025617
[131] -0.326132586 0.354112435 0.797817720 -0.508647052 0.317164893
[136] 0.893685340 -0.408718083 0.346260595 0.142502264 -0.086496276
[141] -0.833993296 0.302655669 -0.216245487 -0.873299745 0.325425776
[146] -0.113109722 -0.497896434 0.416767110 -0.254933317 -0.616783716
> summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Species
setosa :50
versicolor:50
virginica :50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment