Using apply, sapply, and lapply

Original source: http://www.r-bloggers.com/using-apply-sapply-lapply-in-r/

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# Create some data
data <- matrix(data=cbind(rnorm(100, 0), rnorm(100, 25), rnorm(100, 50)), nrow=100, ncol=3)

Apply

The apply function performs some action on a block of values. Note, all the values must be of the same data type (e.g. numeric, string, etc.). If they contain both numeric and string, the numeric values will be converted into strings before the function is applied.

# Find mean value of each row (denoted by "1")
apply(data, 1, mean)
  [1] 24.59901 26.20459 25.24876 24.70247 24.16932 24.81458 24.52746 25.41626
  [9] 25.46790 25.10375 24.70040 24.45695 25.34161 25.13964 24.75298 25.19169
 [17] 24.23966 25.04549 25.79292 23.79149 25.14577 24.69546 25.24540 24.63793
 [25] 25.25698 25.16858 24.98593 25.76139 24.90628 24.99306 24.62098 25.04368
 [33] 24.46879 25.25711 25.18968 24.73647 25.36020 24.75081 24.71036 25.78176
 [41] 23.90849 24.28943 24.96408 23.59350 25.23699 24.64131 25.20141 24.77597
 [49] 25.35063 24.94441 25.32127 26.06588 24.89428 25.04112 24.10786 25.55573
 [57] 25.01078 24.43195 25.15557 24.85495 25.07362 24.83866 25.13600 25.20097
 [65] 24.86642 23.31778 25.00892 25.03551 25.21849 25.14255 26.10872 25.98042
 [73] 25.56874 25.23941 24.29674 24.91502 24.49740 24.61255 24.14655 24.59718
 [81] 24.90140 25.91023 24.92573 24.23798 24.52005 25.66280 24.22381 25.19263
 [89] 24.54775 24.96474 24.50890 25.30642 25.47648 24.69407 25.57084 24.71331
 [97] 25.22384 24.40210 25.25857 24.73543
# Find mean value of each column (denoted by "2")
apply(data, 2, mean)
[1]  0.09619052 24.89715930 49.84308683
# Find mean of reach additional value. That is, apply the mean() functions to each individual observation instead of entire rows or entire columns (denoted by "1:2")
apply(data, 1:2, mean)
               [,1]     [,2]     [,3]
  [1,] -1.568708977 25.19731 50.16843
  [2,]  1.150529977 25.82535 51.63790
  [3,]  0.977275865 24.50395 50.26505
  [4,] -0.263941077 24.75872 49.61265
  [5,] -0.757359741 24.43448 48.83085
  [6,]  0.632602125 24.57053 49.24061
  [7,] -0.244478069 24.63318 49.19367
  [8,] -0.614889100 25.05391 51.80975
  [9,] -0.592501893 26.41062 50.58558
 [10,]  0.052256014 24.92898 50.33003
 [11,] -0.507014529 24.75101 49.85721
 [12,]  0.473880143 24.26357 48.63340
 [13,]  2.615240491 25.42869 47.98090
 [14,] -0.350429812 25.74276 50.02659
 [15,]  0.661601288 24.33628 49.26105
 [16,]  0.797786704 24.39170 50.38559
 [17,] -1.015018178 23.70737 50.02661
 [18,]  0.331179662 25.62967 49.17563
 [19,]  1.255608573 25.30459 50.81856
 [20,] -2.162252797 25.38918 48.14754
 [21,]  0.525101996 24.14938 50.76283
 [22,]  0.247228755 24.87424 48.96490
 [23,]  1.196517482 24.49384 50.04583
 [24,]  0.588874298 23.34821 49.97671
 [25,] -0.902219593 25.60770 51.06546
 [26,] -0.383807172 26.20012 49.68944
 [27,] -1.427233324 25.75519 50.62983
 [28,]  1.050320147 25.28623 50.94763
 [29,] -0.599947956 26.38403 48.93474
 [30,] -0.302622507 25.55671 49.72508
 [31,]  0.019930217 23.56713 50.27587
 [32,]  0.883376538 25.30232 48.94533
 [33,]  0.840728908 23.36012 49.20553
 [34,]  1.366500062 23.14374 51.26110
 [35,]  0.176682690 25.19876 50.19360
 [36,] -0.811892248 24.28875 50.73254
 [37,] -0.274652977 24.22998 52.12526
 [38,] -0.157275521 24.89746 49.51226
 [39,]  0.132177317 24.87886 49.12005
 [40,]  1.136077454 25.41962 50.78960
 [41,] -1.300481517 24.50820 48.51774
 [42,] -0.383029453 22.84781 50.40350
 [43,]  0.099370222 25.41390 49.37896
 [44,] -2.050115390 23.07011 49.76051
 [45,]  0.501352187 25.41343 49.79617
 [46,]  0.009760826 24.30647 49.60769
 [47,] -0.064267636 25.58423 50.08428
 [48,]  0.317009746 24.11671 49.89420
 [49,] -0.229239652 25.26184 51.01928
 [50,]  0.458235055 24.48696 49.88805
 [51,]  0.076155053 25.44080 50.44686
 [52,]  0.734427587 26.65986 50.80336
 [53,] -0.084391861 25.56510 49.20214
 [54,] -0.400739985 25.20466 50.31946
 [55,]  0.620316321 23.12588 48.57738
 [56,] -0.369067753 26.46223 50.57403
 [57,]  0.583869219 24.85391 49.59456
 [58,] -1.012146164 23.52167 50.78632
 [59,] -0.176942984 25.33134 50.31231
 [60,]  1.031038799 23.63650 49.89732
 [61,]  0.291820274 24.04088 50.88817
 [62,] -0.286363289 26.31617 48.48616
 [63,]  0.295092879 25.12000 49.99292
 [64,]  1.738129994 25.47344 48.39135
 [65,] -1.444422908 25.03078 51.01289
 [66,] -1.992472026 23.99107 47.95475
 [67,] -0.126062864 24.88056 50.27226
 [68,]  1.511624181 24.90431 48.69059
 [69,]  0.407953425 24.83007 50.41746
 [70,]  0.729027047 25.18040 49.51824
 [71,]  1.614534378 26.13329 50.57833
 [72,]  1.528257134 25.48461 50.92839
 [73,]  0.185587393 25.58042 50.94020
 [74,]  0.035201742 25.84610 49.83694
 [75,] -0.220080957 25.49684 47.61347
 [76,]  0.751172599 24.12418 49.86970
 [77,] -0.706358939 24.17086 50.02771
 [78,] -0.748064620 25.79963 48.78608
 [79,] -0.763060888 23.32252 49.88018
 [80,]  0.813353831 24.79051 48.18769
 [81,]  0.327942825 23.91075 50.46549
 [82,]  2.386272878 25.54947 49.79496
 [83,]  1.099094285 25.26427 48.41382
 [84,]  0.487128984 24.79840 47.42842
 [85,] -0.921897261 25.64221 48.83984
 [86,]  1.619382465 24.87703 50.49199
 [87,] -2.430495109 25.74597 49.35596
 [88,]  0.136899216 25.98202 49.45896
 [89,] -1.055246874 23.77861 50.91988
 [90,] -0.691470394 24.56741 51.01827
 [91,]  0.019588244 24.59630 48.91080
 [92,] -0.070343647 25.54222 50.44737
 [93,]  0.432749908 24.74199 51.25470
 [94,]  0.678025865 23.27195 50.13224
 [95,]  0.405506770 27.02547 49.28155
 [96,]  1.652979613 23.30071 49.18624
 [97,]  1.006041068 24.88392 49.78156
 [98,] -0.425817664 25.72349 47.90862
 [99,] -0.437871402 25.73701 50.47658
[100,] -0.750629736 24.24426 50.71267

Sapply

# Sapply applies a function over each individual element in an object, returning a vector
sapply(data, mean)
  [1] -1.568708977  1.150529977  0.977275865 -0.263941077 -0.757359741
  [6]  0.632602125 -0.244478069 -0.614889100 -0.592501893  0.052256014
 [11] -0.507014529  0.473880143  2.615240491 -0.350429812  0.661601288
 [16]  0.797786704 -1.015018178  0.331179662  1.255608573 -2.162252797
 [21]  0.525101996  0.247228755  1.196517482  0.588874298 -0.902219593
 [26] -0.383807172 -1.427233324  1.050320147 -0.599947956 -0.302622507
 [31]  0.019930217  0.883376538  0.840728908  1.366500062  0.176682690
 [36] -0.811892248 -0.274652977 -0.157275521  0.132177317  1.136077454
 [41] -1.300481517 -0.383029453  0.099370222 -2.050115390  0.501352187
 [46]  0.009760826 -0.064267636  0.317009746 -0.229239652  0.458235055
 [51]  0.076155053  0.734427587 -0.084391861 -0.400739985  0.620316321
 [56] -0.369067753  0.583869219 -1.012146164 -0.176942984  1.031038799
 [61]  0.291820274 -0.286363289  0.295092879  1.738129994 -1.444422908
 [66] -1.992472026 -0.126062864  1.511624181  0.407953425  0.729027047
 [71]  1.614534378  1.528257134  0.185587393  0.035201742 -0.220080957
 [76]  0.751172599 -0.706358939 -0.748064620 -0.763060888  0.813353831
 [81]  0.327942825  2.386272878  1.099094285  0.487128984 -0.921897261
 [86]  1.619382465 -2.430495109  0.136899216 -1.055246874 -0.691470394
 [91]  0.019588244 -0.070343647  0.432749908  0.678025865  0.405506770
 [96]  1.652979613  1.006041068 -0.425817664 -0.437871402 -0.750629736
[101] 25.197306545 25.825350781 24.503953832 24.758718674 24.434477602
[106] 24.570526558 24.633180740 25.053909206 26.410619962 24.928981884
[111] 24.751005799 24.263566175 25.428692964 25.742763722 24.336277408
[116] 24.391703303 23.707374457 25.629673993 25.304587483 25.389176630
[121] 24.149383170 24.874239009 24.493836724 23.348211090 25.607700571
[126] 26.200116013 25.755185549 25.286226101 26.384032256 25.556712376
[131] 23.567129337 25.302324415 23.360122623 23.143743252 25.198758203
[136] 24.288745427 24.229984320 24.897463124 24.878858415 25.419617225
[141] 24.508196500 22.847811945 25.413903990 23.070107275 25.413433368
[146] 24.306468586 25.584229464 24.116708141 25.261839601 24.486956445
[151] 25.440800049 26.659857557 25.565101945 25.204656283 23.125881246
[156] 26.462228263 24.853909780 23.521668193 25.331337679 23.636499726
[161] 24.040880917 26.316172578 25.119996193 25.473437131 25.030778969
[166] 23.991070971 24.880564659 24.904307929 24.830074007 25.180398308
[171] 26.133289979 25.484612200 25.580424723 25.846103841 25.496839595
[176] 24.124179656 24.170857114 25.799628759 23.322523521 24.790511111
[181] 23.910751115 25.549466633 25.264265733 24.798400630 25.642209925
[186] 24.877027823 25.745973119 25.982021165 23.778612923 24.567407556
[191] 24.596298326 25.542218418 24.741988971 23.271952314 27.025469544
[196] 23.300710553 24.883916973 25.723485456 25.737009580 24.244258255
[201] 50.168431849 51.637900971 50.265045566 49.612645844 48.830848358
[206] 49.240613619 49.193669941 51.809750441 50.585582341 50.330025798
[211] 49.857207672 48.633404522 47.980896119 50.026593960 49.261052113
[216] 50.385585194 50.026611946 49.175626026 50.818555563 48.147539082
[221] 50.762826545 48.964901402 50.045832988 49.976713639 51.065456311
[226] 49.689440608 50.629825924 50.947626860 48.934744168 49.725079985
[231] 50.275872304 48.945331047 49.205531669 51.261095381 50.193603345
[236] 50.732543196 52.125259556 49.512255163 49.120047270 50.789598761
[241] 48.517741319 50.403500715 49.378962367 49.760507109 49.796171242
[246] 49.607690616 50.084275217 49.894199065 51.019282225 49.888051966
[251] 50.446861713 50.803355703 49.202143279 50.319458013 48.577378982
[256] 50.574032023 49.594563809 50.786319423 50.312312319 49.897318172
[261] 50.888168155 48.486159534 49.992922240 48.391347666 51.012893420
[266] 47.954752646 50.272263973 48.690586502 50.417455451 49.518237586
[271] 50.578329878 50.928392627 50.940201968 49.836937110 47.613474293
[276] 49.869701038 50.027708980 48.786075248 49.880176985 48.187685772
[281] 50.465491452 49.794963712 48.413817125 47.428420395 48.839838668
[286] 50.491987788 49.355956225 49.458958967 50.919880652 51.018268137
[291] 48.910802035 50.447373239 51.254699133 50.132239055 49.281546656
[296] 49.186239840 49.781557088 47.908620364 50.476582295 50.712671097

Lapply

Lapply applies a function of each individual element in an object, returning a list

# Create a list with two elements
l <- list(a = 1:10, b = 11:20)

# Apply the sum function to each list
lapply(l, sum)
$a
[1] 55

$b
[1] 155