layout: true .absolute.top-0.right-1.tr.w-10[ ![](https://raw.githubusercontent.com/jmbuhr/dataIntro20/master/images/hex.png)<!-- --> ] --- name: title class: left bottom hide-count background-color: #FBFCFF;
Introduction to Data Analysis with R
Lecture 3: Tidy Data and Iteration
Jannik Buhr
Heidelberg University, WS20/21
2020-11-14
.absolute.bottom-0.right-1.mid-gray[ With Artwork by @allison_horst ] --- class: center, middle ## Tidy Data with `tidyr` <a href="https://tidyr.tidyverse.org/"> <img src="img/tidyr.png" width="107" class=external /> </a> --- ## Tidy Data > »Happy families are all alike;<br> every unhappy family is unhappy in its own way.«<br> — Leo Tolstoy -- <br> > »Tidy datasets are all alike,<br> but every messy dataset is messy in its own way.«<br> — Hadley Wickham -- <div class="figure"> <img src="img/tidy-data-wickham.png" alt="Figure from https://r4ds.had.co.nz/tidy-data.html" width="853" class=external /> <p class="caption">Figure from https://r4ds.had.co.nz/tidy-data.html</p> </div> --- ## `table1` <br> .w-50[ <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":["country"],"name":[1],"type":["chr"],"align":["left"]},{"label":["year"],"name":[2],"type":["int"],"align":["right"]},{"label":["cases"],"name":[3],"type":["int"],"align":["right"]},{"label":["population"],"name":[4],"type":["int"],"align":["right"]}],"data":[{"1":"Afghanistan","2":"1999","3":"745","4":"19987071"},{"1":"Afghanistan","2":"2000","3":"2666","4":"20595360"},{"1":"Brazil","2":"1999","3":"37737","4":"172006362"},{"1":"Brazil","2":"2000","3":"80488","4":"174504898"},{"1":"China","2":"1999","3":"212258","4":"1272915272"},{"1":"China","2":"2000","3":"213766","4":"1280428583"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}} </script> </div> ] - Number of TB cases documented by the World Health Organization in Afghanistan, Brazil, and China between 1999 and 2000. - The first of these is in the tidy format, the others are not: --- ## `table2` <br> .w-50[ <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":["country"],"name":[1],"type":["chr"],"align":["left"]},{"label":["year"],"name":[2],"type":["int"],"align":["right"]},{"label":["type"],"name":[3],"type":["chr"],"align":["left"]},{"label":["count"],"name":[4],"type":["int"],"align":["right"]}],"data":[{"1":"Afghanistan","2":"1999","3":"cases","4":"745"},{"1":"Afghanistan","2":"1999","3":"population","4":"19987071"},{"1":"Afghanistan","2":"2000","3":"cases","4":"2666"},{"1":"Afghanistan","2":"2000","3":"population","4":"20595360"},{"1":"Brazil","2":"1999","3":"cases","4":"37737"},{"1":"Brazil","2":"1999","3":"population","4":"172006362"},{"1":"Brazil","2":"2000","3":"cases","4":"80488"},{"1":"Brazil","2":"2000","3":"population","4":"174504898"},{"1":"China","2":"1999","3":"cases","4":"212258"},{"1":"China","2":"1999","3":"population","4":"1272915272"},{"1":"China","2":"2000","3":"cases","4":"213766"},{"1":"China","2":"2000","3":"population","4":"1280428583"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}} </script> </div> ] --- ## `table3` <br> .w-50[ <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["country"],"name":[1],"type":["chr"],"align":["left"]},{"label":["year"],"name":[2],"type":["int"],"align":["right"]},{"label":["rate"],"name":[3],"type":["chr"],"align":["left"]}],"data":[{"1":"Afghanistan","2":"1999","3":"745/19987071","_rn_":"1"},{"1":"Afghanistan","2":"2000","3":"2666/20595360","_rn_":"2"},{"1":"Brazil","2":"1999","3":"37737/172006362","_rn_":"3"},{"1":"Brazil","2":"2000","3":"80488/174504898","_rn_":"4"},{"1":"China","2":"1999","3":"212258/1272915272","_rn_":"5"},{"1":"China","2":"2000","3":"213766/1280428583","_rn_":"6"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}} </script> </div> ] --- ## `table4a` and `table4b` <br> .pull-left.w-40[ <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["country"],"name":[1],"type":["chr"],"align":["left"]},{"label":["1999"],"name":[2],"type":["int"],"align":["right"]},{"label":["2000"],"name":[3],"type":["int"],"align":["right"]}],"data":[{"1":"Afghanistan","2":"745","3":"2666","_rn_":"1"},{"1":"Brazil","2":"37737","3":"80488","_rn_":"2"},{"1":"China","2":"212258","3":"213766","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}} </script> </div> ] .pull-right.w-40[ <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["country"],"name":[1],"type":["chr"],"align":["left"]},{"label":["1999"],"name":[2],"type":["int"],"align":["right"]},{"label":["2000"],"name":[3],"type":["int"],"align":["right"]}],"data":[{"1":"Afghanistan","2":"19987071","3":"20595360","_rn_":"1"},{"1":"Brazil","2":"172006362","3":"174504898","_rn_":"2"},{"1":"China","2":"1272915272","3":"1280428583","_rn_":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}} </script> </div> ] --- ## `table5` <br> .w-50[ <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["country"],"name":[1],"type":["chr"],"align":["left"]},{"label":["century"],"name":[2],"type":["chr"],"align":["left"]},{"label":["year"],"name":[3],"type":["chr"],"align":["left"]},{"label":["rate"],"name":[4],"type":["chr"],"align":["left"]}],"data":[{"1":"Afghanistan","2":"19","3":"99","4":"745/19987071","_rn_":"1"},{"1":"Afghanistan","2":"20","3":"00","4":"2666/20595360","_rn_":"2"},{"1":"Brazil","2":"19","3":"99","4":"37737/172006362","_rn_":"3"},{"1":"Brazil","2":"20","3":"00","4":"80488/174504898","_rn_":"4"},{"1":"China","2":"19","3":"99","4":"212258/1272915272","_rn_":"5"},{"1":"China","2":"20","3":"00","4":"213766/1280428583","_rn_":"6"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}} </script> </div> ]