Created
August 24, 2020 11:03
-
-
Save PatrickRWright/d30759acb4b11da5c1af4ac625ab4bc1 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Using the map function with tilde (~) notations" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"In part inspired by [this](https://towardsdatascience.com/functional-programming-in-r-with-purrr-469e597d0229)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# %>%\n", | |
"library(magrittr)\n", | |
"# map\n", | |
"library(purrr)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Map will apply a given function on each element of the input. The **dot (.)** indicated the object passed through by the pipe. In this case first the complete iris dataset and then a list of three data frames. One for each species in the iris dataset." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$setosa</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>5.006</li><li>3.428</li></ol>\n", | |
"</dd>\n", | |
"\t<dt>$versicolor</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>5.936</li><li>2.77</li></ol>\n", | |
"</dd>\n", | |
"\t<dt>$virginica</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>6.588</li><li>2.974</li></ol>\n", | |
"</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$setosa] \\begin{enumerate*}\n", | |
"\\item 5.006\n", | |
"\\item 3.428\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\item[\\$versicolor] \\begin{enumerate*}\n", | |
"\\item 5.936\n", | |
"\\item 2.77\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\item[\\$virginica] \\begin{enumerate*}\n", | |
"\\item 6.588\n", | |
"\\item 2.974\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$setosa\n", | |
": 1. 5.006\n", | |
"2. 3.428\n", | |
"\n", | |
"\n", | |
"\n", | |
"$versicolor\n", | |
": 1. 5.936\n", | |
"2. 2.77\n", | |
"\n", | |
"\n", | |
"\n", | |
"$virginica\n", | |
": 1. 6.588\n", | |
"2. 2.974\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$setosa\n", | |
"[1] 5.006 3.428\n", | |
"\n", | |
"$versicolor\n", | |
"[1] 5.936 2.770\n", | |
"\n", | |
"$virginica\n", | |
"[1] 6.588 2.974\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# split iris into a list by the different species and get the means for Sepal.Length and Sepal.Width\n", | |
"iris %>%\n", | |
" split(.$Species) %>%\n", | |
" map(~ c(mean(.$Sepal.Length),\n", | |
" mean(.$Sepal.Width)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The **tilde (~)** statement in the **map** call is equivalent to the following individual function and can thus replace the overhead of predefining a function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"means_sepal_iris <- function(data) {\n", | |
" return(c(mean(data$Sepal.Length),\n", | |
" mean(data$Sepal.Width)))\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$setosa</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>5.006</li><li>3.428</li></ol>\n", | |
"</dd>\n", | |
"\t<dt>$versicolor</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>5.936</li><li>2.77</li></ol>\n", | |
"</dd>\n", | |
"\t<dt>$virginica</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>6.588</li><li>2.974</li></ol>\n", | |
"</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$setosa] \\begin{enumerate*}\n", | |
"\\item 5.006\n", | |
"\\item 3.428\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\item[\\$versicolor] \\begin{enumerate*}\n", | |
"\\item 5.936\n", | |
"\\item 2.77\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\item[\\$virginica] \\begin{enumerate*}\n", | |
"\\item 6.588\n", | |
"\\item 2.974\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$setosa\n", | |
": 1. 5.006\n", | |
"2. 3.428\n", | |
"\n", | |
"\n", | |
"\n", | |
"$versicolor\n", | |
": 1. 5.936\n", | |
"2. 2.77\n", | |
"\n", | |
"\n", | |
"\n", | |
"$virginica\n", | |
": 1. 6.588\n", | |
"2. 2.974\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$setosa\n", | |
"[1] 5.006 3.428\n", | |
"\n", | |
"$versicolor\n", | |
"[1] 5.936 2.770\n", | |
"\n", | |
"$virginica\n", | |
"[1] 6.588 2.974\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"iris %>%\n", | |
" split(.$Species) %>%\n", | |
" map(means_sepal_iris)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<dl>\n", | |
"\t<dt>$setosa</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>5.006</li><li>3.428</li></ol>\n", | |
"</dd>\n", | |
"\t<dt>$versicolor</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>5.936</li><li>2.77</li></ol>\n", | |
"</dd>\n", | |
"\t<dt>$virginica</dt>\n", | |
"\t\t<dd><style>\n", | |
".list-inline {list-style: none; margin:0; padding: 0}\n", | |
".list-inline>li {display: inline-block}\n", | |
".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n", | |
"</style>\n", | |
"<ol class=list-inline><li>6.588</li><li>2.974</li></ol>\n", | |
"</dd>\n", | |
"</dl>\n" | |
], | |
"text/latex": [ | |
"\\begin{description}\n", | |
"\\item[\\$setosa] \\begin{enumerate*}\n", | |
"\\item 5.006\n", | |
"\\item 3.428\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\item[\\$versicolor] \\begin{enumerate*}\n", | |
"\\item 5.936\n", | |
"\\item 2.77\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\item[\\$virginica] \\begin{enumerate*}\n", | |
"\\item 6.588\n", | |
"\\item 2.974\n", | |
"\\end{enumerate*}\n", | |
"\n", | |
"\\end{description}\n" | |
], | |
"text/markdown": [ | |
"$setosa\n", | |
": 1. 5.006\n", | |
"2. 3.428\n", | |
"\n", | |
"\n", | |
"\n", | |
"$versicolor\n", | |
": 1. 5.936\n", | |
"2. 2.77\n", | |
"\n", | |
"\n", | |
"\n", | |
"$virginica\n", | |
": 1. 6.588\n", | |
"2. 2.974\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n" | |
], | |
"text/plain": [ | |
"$setosa\n", | |
"[1] 5.006 3.428\n", | |
"\n", | |
"$versicolor\n", | |
"[1] 5.936 2.770\n", | |
"\n", | |
"$virginica\n", | |
"[1] 6.588 2.974\n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"iris %>%\n", | |
" split(.$Species) %>%\n", | |
" map(function(.) { return(c(mean(.$Sepal.Length),\n", | |
" mean(.$Sepal.Width)))}\n", | |
" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"TRUE" | |
], | |
"text/latex": [ | |
"TRUE" | |
], | |
"text/markdown": [ | |
"TRUE" | |
], | |
"text/plain": [ | |
"[1] TRUE" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"TRUE" | |
], | |
"text/latex": [ | |
"TRUE" | |
], | |
"text/markdown": [ | |
"TRUE" | |
], | |
"text/plain": [ | |
"[1] TRUE" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"TRUE" | |
], | |
"text/latex": [ | |
"TRUE" | |
], | |
"text/markdown": [ | |
"TRUE" | |
], | |
"text/plain": [ | |
"[1] TRUE" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# Some sanity checks\n", | |
"mean(iris$Sepal.Length[which(iris$Species == \"setosa\")]) == 5.006\n", | |
"mean(iris$Sepal.Length[which(iris$Species == \"versicolor\")]) == 5.936\n", | |
"mean(iris$Sepal.Width[which(iris$Species == \"virginica\")]) == 2.974" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"R version 3.5.1 (2018-07-02)\n", | |
"Platform: x86_64-conda_cos6-linux-gnu (64-bit)\n", | |
"Running under: Debian GNU/Linux 10 (buster)\n", | |
"\n", | |
"Matrix products: default\n", | |
"BLAS/LAPACK: /home/jupyterlab/conda/envs/r/lib/R/lib/libRlapack.so\n", | |
"\n", | |
"locale:\n", | |
" [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 \n", | |
" [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8 \n", | |
" [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C \n", | |
"[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C \n", | |
"\n", | |
"attached base packages:\n", | |
"[1] stats graphics grDevices utils datasets methods base \n", | |
"\n", | |
"other attached packages:\n", | |
"[1] purrr_0.3.4 magrittr_1.5\n", | |
"\n", | |
"loaded via a namespace (and not attached):\n", | |
" [1] Rcpp_1.0.4.6 digest_0.6.25 crayon_1.3.4 IRdisplay_0.7.0\n", | |
" [5] repr_1.1.0 jsonlite_1.6.1 evaluate_0.14 pillar_1.4.3 \n", | |
" [9] rlang_0.4.5 uuid_0.1-4 IRkernel_0.8.12 tools_3.5.1 \n", | |
"[13] compiler_3.5.1 base64enc_0.1-3 htmltools_0.4.0 pbdZMQ_0.3-3 " | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"sessionInfo()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "R", | |
"language": "R", | |
"name": "conda-env-r-r" | |
}, | |
"language_info": { | |
"codemirror_mode": "r", | |
"file_extension": ".r", | |
"mimetype": "text/x-r-source", | |
"name": "R", | |
"pygments_lexer": "r", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment