{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using apply, sapply, and lapply\n",
    "\n",
    "Original source: http://www.r-bloggers.com/using-apply-sapply-lapply-in-r/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create some data\n",
    "data <- matrix(data=cbind(rnorm(100, 0), rnorm(100, 25), rnorm(100, 50)), nrow=100, ncol=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Apply\n",
    "\n",
    "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  [1] 24.59901 26.20459 25.24876 24.70247 24.16932 24.81458 24.52746 25.41626\n",
       "  [9] 25.46790 25.10375 24.70040 24.45695 25.34161 25.13964 24.75298 25.19169\n",
       " [17] 24.23966 25.04549 25.79292 23.79149 25.14577 24.69546 25.24540 24.63793\n",
       " [25] 25.25698 25.16858 24.98593 25.76139 24.90628 24.99306 24.62098 25.04368\n",
       " [33] 24.46879 25.25711 25.18968 24.73647 25.36020 24.75081 24.71036 25.78176\n",
       " [41] 23.90849 24.28943 24.96408 23.59350 25.23699 24.64131 25.20141 24.77597\n",
       " [49] 25.35063 24.94441 25.32127 26.06588 24.89428 25.04112 24.10786 25.55573\n",
       " [57] 25.01078 24.43195 25.15557 24.85495 25.07362 24.83866 25.13600 25.20097\n",
       " [65] 24.86642 23.31778 25.00892 25.03551 25.21849 25.14255 26.10872 25.98042\n",
       " [73] 25.56874 25.23941 24.29674 24.91502 24.49740 24.61255 24.14655 24.59718\n",
       " [81] 24.90140 25.91023 24.92573 24.23798 24.52005 25.66280 24.22381 25.19263\n",
       " [89] 24.54775 24.96474 24.50890 25.30642 25.47648 24.69407 25.57084 24.71331\n",
       " [97] 25.22384 24.40210 25.25857 24.73543"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Find mean value of each row (denoted by \"1\")\n",
    "apply(data, 1, mean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1]  0.09619052 24.89715930 49.84308683"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Find mean value of each column (denoted by \"2\")\n",
    "apply(data, 2, mean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "               [,1]     [,2]     [,3]\n",
       "  [1,] -1.568708977 25.19731 50.16843\n",
       "  [2,]  1.150529977 25.82535 51.63790\n",
       "  [3,]  0.977275865 24.50395 50.26505\n",
       "  [4,] -0.263941077 24.75872 49.61265\n",
       "  [5,] -0.757359741 24.43448 48.83085\n",
       "  [6,]  0.632602125 24.57053 49.24061\n",
       "  [7,] -0.244478069 24.63318 49.19367\n",
       "  [8,] -0.614889100 25.05391 51.80975\n",
       "  [9,] -0.592501893 26.41062 50.58558\n",
       " [10,]  0.052256014 24.92898 50.33003\n",
       " [11,] -0.507014529 24.75101 49.85721\n",
       " [12,]  0.473880143 24.26357 48.63340\n",
       " [13,]  2.615240491 25.42869 47.98090\n",
       " [14,] -0.350429812 25.74276 50.02659\n",
       " [15,]  0.661601288 24.33628 49.26105\n",
       " [16,]  0.797786704 24.39170 50.38559\n",
       " [17,] -1.015018178 23.70737 50.02661\n",
       " [18,]  0.331179662 25.62967 49.17563\n",
       " [19,]  1.255608573 25.30459 50.81856\n",
       " [20,] -2.162252797 25.38918 48.14754\n",
       " [21,]  0.525101996 24.14938 50.76283\n",
       " [22,]  0.247228755 24.87424 48.96490\n",
       " [23,]  1.196517482 24.49384 50.04583\n",
       " [24,]  0.588874298 23.34821 49.97671\n",
       " [25,] -0.902219593 25.60770 51.06546\n",
       " [26,] -0.383807172 26.20012 49.68944\n",
       " [27,] -1.427233324 25.75519 50.62983\n",
       " [28,]  1.050320147 25.28623 50.94763\n",
       " [29,] -0.599947956 26.38403 48.93474\n",
       " [30,] -0.302622507 25.55671 49.72508\n",
       " [31,]  0.019930217 23.56713 50.27587\n",
       " [32,]  0.883376538 25.30232 48.94533\n",
       " [33,]  0.840728908 23.36012 49.20553\n",
       " [34,]  1.366500062 23.14374 51.26110\n",
       " [35,]  0.176682690 25.19876 50.19360\n",
       " [36,] -0.811892248 24.28875 50.73254\n",
       " [37,] -0.274652977 24.22998 52.12526\n",
       " [38,] -0.157275521 24.89746 49.51226\n",
       " [39,]  0.132177317 24.87886 49.12005\n",
       " [40,]  1.136077454 25.41962 50.78960\n",
       " [41,] -1.300481517 24.50820 48.51774\n",
       " [42,] -0.383029453 22.84781 50.40350\n",
       " [43,]  0.099370222 25.41390 49.37896\n",
       " [44,] -2.050115390 23.07011 49.76051\n",
       " [45,]  0.501352187 25.41343 49.79617\n",
       " [46,]  0.009760826 24.30647 49.60769\n",
       " [47,] -0.064267636 25.58423 50.08428\n",
       " [48,]  0.317009746 24.11671 49.89420\n",
       " [49,] -0.229239652 25.26184 51.01928\n",
       " [50,]  0.458235055 24.48696 49.88805\n",
       " [51,]  0.076155053 25.44080 50.44686\n",
       " [52,]  0.734427587 26.65986 50.80336\n",
       " [53,] -0.084391861 25.56510 49.20214\n",
       " [54,] -0.400739985 25.20466 50.31946\n",
       " [55,]  0.620316321 23.12588 48.57738\n",
       " [56,] -0.369067753 26.46223 50.57403\n",
       " [57,]  0.583869219 24.85391 49.59456\n",
       " [58,] -1.012146164 23.52167 50.78632\n",
       " [59,] -0.176942984 25.33134 50.31231\n",
       " [60,]  1.031038799 23.63650 49.89732\n",
       " [61,]  0.291820274 24.04088 50.88817\n",
       " [62,] -0.286363289 26.31617 48.48616\n",
       " [63,]  0.295092879 25.12000 49.99292\n",
       " [64,]  1.738129994 25.47344 48.39135\n",
       " [65,] -1.444422908 25.03078 51.01289\n",
       " [66,] -1.992472026 23.99107 47.95475\n",
       " [67,] -0.126062864 24.88056 50.27226\n",
       " [68,]  1.511624181 24.90431 48.69059\n",
       " [69,]  0.407953425 24.83007 50.41746\n",
       " [70,]  0.729027047 25.18040 49.51824\n",
       " [71,]  1.614534378 26.13329 50.57833\n",
       " [72,]  1.528257134 25.48461 50.92839\n",
       " [73,]  0.185587393 25.58042 50.94020\n",
       " [74,]  0.035201742 25.84610 49.83694\n",
       " [75,] -0.220080957 25.49684 47.61347\n",
       " [76,]  0.751172599 24.12418 49.86970\n",
       " [77,] -0.706358939 24.17086 50.02771\n",
       " [78,] -0.748064620 25.79963 48.78608\n",
       " [79,] -0.763060888 23.32252 49.88018\n",
       " [80,]  0.813353831 24.79051 48.18769\n",
       " [81,]  0.327942825 23.91075 50.46549\n",
       " [82,]  2.386272878 25.54947 49.79496\n",
       " [83,]  1.099094285 25.26427 48.41382\n",
       " [84,]  0.487128984 24.79840 47.42842\n",
       " [85,] -0.921897261 25.64221 48.83984\n",
       " [86,]  1.619382465 24.87703 50.49199\n",
       " [87,] -2.430495109 25.74597 49.35596\n",
       " [88,]  0.136899216 25.98202 49.45896\n",
       " [89,] -1.055246874 23.77861 50.91988\n",
       " [90,] -0.691470394 24.56741 51.01827\n",
       " [91,]  0.019588244 24.59630 48.91080\n",
       " [92,] -0.070343647 25.54222 50.44737\n",
       " [93,]  0.432749908 24.74199 51.25470\n",
       " [94,]  0.678025865 23.27195 50.13224\n",
       " [95,]  0.405506770 27.02547 49.28155\n",
       " [96,]  1.652979613 23.30071 49.18624\n",
       " [97,]  1.006041068 24.88392 49.78156\n",
       " [98,] -0.425817664 25.72349 47.90862\n",
       " [99,] -0.437871402 25.73701 50.47658\n",
       "[100,] -0.750629736 24.24426 50.71267"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 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\")\n",
    "apply(data, 1:2, mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sapply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  [1] -1.568708977  1.150529977  0.977275865 -0.263941077 -0.757359741\n",
       "  [6]  0.632602125 -0.244478069 -0.614889100 -0.592501893  0.052256014\n",
       " [11] -0.507014529  0.473880143  2.615240491 -0.350429812  0.661601288\n",
       " [16]  0.797786704 -1.015018178  0.331179662  1.255608573 -2.162252797\n",
       " [21]  0.525101996  0.247228755  1.196517482  0.588874298 -0.902219593\n",
       " [26] -0.383807172 -1.427233324  1.050320147 -0.599947956 -0.302622507\n",
       " [31]  0.019930217  0.883376538  0.840728908  1.366500062  0.176682690\n",
       " [36] -0.811892248 -0.274652977 -0.157275521  0.132177317  1.136077454\n",
       " [41] -1.300481517 -0.383029453  0.099370222 -2.050115390  0.501352187\n",
       " [46]  0.009760826 -0.064267636  0.317009746 -0.229239652  0.458235055\n",
       " [51]  0.076155053  0.734427587 -0.084391861 -0.400739985  0.620316321\n",
       " [56] -0.369067753  0.583869219 -1.012146164 -0.176942984  1.031038799\n",
       " [61]  0.291820274 -0.286363289  0.295092879  1.738129994 -1.444422908\n",
       " [66] -1.992472026 -0.126062864  1.511624181  0.407953425  0.729027047\n",
       " [71]  1.614534378  1.528257134  0.185587393  0.035201742 -0.220080957\n",
       " [76]  0.751172599 -0.706358939 -0.748064620 -0.763060888  0.813353831\n",
       " [81]  0.327942825  2.386272878  1.099094285  0.487128984 -0.921897261\n",
       " [86]  1.619382465 -2.430495109  0.136899216 -1.055246874 -0.691470394\n",
       " [91]  0.019588244 -0.070343647  0.432749908  0.678025865  0.405506770\n",
       " [96]  1.652979613  1.006041068 -0.425817664 -0.437871402 -0.750629736\n",
       "[101] 25.197306545 25.825350781 24.503953832 24.758718674 24.434477602\n",
       "[106] 24.570526558 24.633180740 25.053909206 26.410619962 24.928981884\n",
       "[111] 24.751005799 24.263566175 25.428692964 25.742763722 24.336277408\n",
       "[116] 24.391703303 23.707374457 25.629673993 25.304587483 25.389176630\n",
       "[121] 24.149383170 24.874239009 24.493836724 23.348211090 25.607700571\n",
       "[126] 26.200116013 25.755185549 25.286226101 26.384032256 25.556712376\n",
       "[131] 23.567129337 25.302324415 23.360122623 23.143743252 25.198758203\n",
       "[136] 24.288745427 24.229984320 24.897463124 24.878858415 25.419617225\n",
       "[141] 24.508196500 22.847811945 25.413903990 23.070107275 25.413433368\n",
       "[146] 24.306468586 25.584229464 24.116708141 25.261839601 24.486956445\n",
       "[151] 25.440800049 26.659857557 25.565101945 25.204656283 23.125881246\n",
       "[156] 26.462228263 24.853909780 23.521668193 25.331337679 23.636499726\n",
       "[161] 24.040880917 26.316172578 25.119996193 25.473437131 25.030778969\n",
       "[166] 23.991070971 24.880564659 24.904307929 24.830074007 25.180398308\n",
       "[171] 26.133289979 25.484612200 25.580424723 25.846103841 25.496839595\n",
       "[176] 24.124179656 24.170857114 25.799628759 23.322523521 24.790511111\n",
       "[181] 23.910751115 25.549466633 25.264265733 24.798400630 25.642209925\n",
       "[186] 24.877027823 25.745973119 25.982021165 23.778612923 24.567407556\n",
       "[191] 24.596298326 25.542218418 24.741988971 23.271952314 27.025469544\n",
       "[196] 23.300710553 24.883916973 25.723485456 25.737009580 24.244258255\n",
       "[201] 50.168431849 51.637900971 50.265045566 49.612645844 48.830848358\n",
       "[206] 49.240613619 49.193669941 51.809750441 50.585582341 50.330025798\n",
       "[211] 49.857207672 48.633404522 47.980896119 50.026593960 49.261052113\n",
       "[216] 50.385585194 50.026611946 49.175626026 50.818555563 48.147539082\n",
       "[221] 50.762826545 48.964901402 50.045832988 49.976713639 51.065456311\n",
       "[226] 49.689440608 50.629825924 50.947626860 48.934744168 49.725079985\n",
       "[231] 50.275872304 48.945331047 49.205531669 51.261095381 50.193603345\n",
       "[236] 50.732543196 52.125259556 49.512255163 49.120047270 50.789598761\n",
       "[241] 48.517741319 50.403500715 49.378962367 49.760507109 49.796171242\n",
       "[246] 49.607690616 50.084275217 49.894199065 51.019282225 49.888051966\n",
       "[251] 50.446861713 50.803355703 49.202143279 50.319458013 48.577378982\n",
       "[256] 50.574032023 49.594563809 50.786319423 50.312312319 49.897318172\n",
       "[261] 50.888168155 48.486159534 49.992922240 48.391347666 51.012893420\n",
       "[266] 47.954752646 50.272263973 48.690586502 50.417455451 49.518237586\n",
       "[271] 50.578329878 50.928392627 50.940201968 49.836937110 47.613474293\n",
       "[276] 49.869701038 50.027708980 48.786075248 49.880176985 48.187685772\n",
       "[281] 50.465491452 49.794963712 48.413817125 47.428420395 48.839838668\n",
       "[286] 50.491987788 49.355956225 49.458958967 50.919880652 51.018268137\n",
       "[291] 48.910802035 50.447373239 51.254699133 50.132239055 49.281546656\n",
       "[296] 49.186239840 49.781557088 47.908620364 50.476582295 50.712671097"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Sapply applies a function over each individual element in an object, returning a vector\n",
    "sapply(data, mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lapply\n",
    "\n",
    "Lapply applies a function of each individual element in an object, returning a list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "$a\n",
       "[1] 55\n",
       "\n",
       "$b\n",
       "[1] 155\n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create a list with two elements\n",
    "l <- list(a = 1:10, b = 11:20)\n",
    "\n",
    "# Apply the sum function to each list\n",
    "lapply(l, sum)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
