{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Manipulation in the `tidyverse`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load library" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Registered S3 methods overwritten by 'ggplot2':\n", " method from \n", " [.quosures rlang\n", " c.quosures rlang\n", " print.quosures rlang\n", "── \u001b[1mAttaching packages\u001b[22m ─────────────────────────────────────── tidyverse 1.2.1 ──\n", "\u001b[32m✔\u001b[39m \u001b[34mggplot2\u001b[39m 3.1.1 \u001b[32m✔\u001b[39m \u001b[34mpurrr \u001b[39m 0.3.2\n", "\u001b[32m✔\u001b[39m \u001b[34mtibble \u001b[39m 2.1.2 \u001b[32m✔\u001b[39m \u001b[34mdplyr \u001b[39m 0.8.1\n", "\u001b[32m✔\u001b[39m \u001b[34mtidyr \u001b[39m 0.8.3 \u001b[32m✔\u001b[39m \u001b[34mstringr\u001b[39m 1.4.0\n", "\u001b[32m✔\u001b[39m \u001b[34mreadr \u001b[39m 1.3.1 \u001b[32m✔\u001b[39m \u001b[34mforcats\u001b[39m 0.4.0\n", "── \u001b[1mConflicts\u001b[22m ────────────────────────────────────────── tidyverse_conflicts() ──\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mfilter()\u001b[39m masks \u001b[34mstats\u001b[39m::filter()\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mlag()\u001b[39m masks \u001b[34mstats\u001b[39m::lag()\n" ] } ], "source": [ "library(tidyverse)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What data sets are available?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "help(starwars)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bssic information" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'tbl_df'
  2. \n", "\t
  3. 'tbl'
  4. \n", "\t
  5. 'data.frame'
  6. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'tbl\\_df'\n", "\\item 'tbl'\n", "\\item 'data.frame'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'tbl_df'\n", "2. 'tbl'\n", "3. 'data.frame'\n", "\n", "\n" ], "text/plain": [ "[1] \"tbl_df\" \"tbl\" \"data.frame\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "class(starwars)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 87
  2. \n", "\t
  3. 13
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 87\n", "\\item 13\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 87\n", "2. 13\n", "\n", "\n" ], "text/plain": [ "[1] 87 13" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dim(starwars)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "87" ], "text/latex": [ "87" ], "text/markdown": [ "87" ], "text/plain": [ "[1] 87" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "nrow(starwars)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "13" ], "text/latex": [ "13" ], "text/markdown": [ "13" ], "text/plain": [ "[1] 13" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ncol(starwars)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
Luke Skywalker17277blondfair blue 19maleTatooineHumanRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens Snowspeeder , Imperial Speeder BikeX-wing , Imperial shuttle
C-3PO 16775NA gold yellow112NA TatooineDroidAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope
R2-D2 9632NA white, bluered 33NA Naboo DroidAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t Luke Skywalker & 172 & 77 & blond & fair & blue & 19 & male & Tatooine & Human & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & Snowspeeder , Imperial Speeder Bike & X-wing , Imperial shuttle\\\\\n", "\t C-3PO & 167 & 75 & NA & gold & yellow & 112 & NA & Tatooine & Droid & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope & & \\\\\n", "\t R2-D2 & 96 & 32 & NA & white, blue & red & 33 & NA & Naboo & Droid & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| Luke Skywalker | 172 | 77 | blond | fair | blue | 19 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Snowspeeder , Imperial Speeder Bike | X-wing , Imperial shuttle |\n", "| C-3PO | 167 | 75 | NA | gold | yellow | 112 | NA | Tatooine | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | | |\n", "| R2-D2 | 96 | 32 | NA | white, blue | red | 33 | NA | Naboo | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | | |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color birth_year gender\n", "1 Luke Skywalker 172 77 blond fair blue 19 male \n", "2 C-3PO 167 75 NA gold yellow 112 NA \n", "3 R2-D2 96 32 NA white, blue red 33 NA \n", " homeworld species\n", "1 Tatooine Human \n", "2 Tatooine Droid \n", "3 Naboo Droid \n", " films \n", "1 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", "2 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope \n", "3 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", " vehicles \n", "1 Snowspeeder , Imperial Speeder Bike\n", "2 \n", "3 \n", " starships \n", "1 X-wing , Imperial shuttle\n", "2 \n", "3 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "head(starwars, 3)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
BB8 NANAnone none black NAnone NA DroidThe Force Awakens
Captain Phasma NANAunknownunknownunknownNAfemaleNA NA The Force Awakens
Padmé Amidala 16545brown light brown 46femaleNabooHumanAttack of the Clones, The Phantom Menace , Revenge of the Sith H-type Nubian yacht, Naboo star skiff , Naboo fighter
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t BB8 & NA & NA & none & none & black & NA & none & NA & Droid & The Force Awakens & & \\\\\n", "\t Captain Phasma & NA & NA & unknown & unknown & unknown & NA & female & NA & NA & The Force Awakens & & \\\\\n", "\t Padmé Amidala & 165 & 45 & brown & light & brown & 46 & female & Naboo & Human & Attack of the Clones, The Phantom Menace , Revenge of the Sith & & H-type Nubian yacht, Naboo star skiff , Naboo fighter \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| BB8 | NA | NA | none | none | black | NA | none | NA | Droid | The Force Awakens | | |\n", "| Captain Phasma | NA | NA | unknown | unknown | unknown | NA | female | NA | NA | The Force Awakens | | |\n", "| Padmé Amidala | 165 | 45 | brown | light | brown | 46 | female | Naboo | Human | Attack of the Clones, The Phantom Menace , Revenge of the Sith | | H-type Nubian yacht, Naboo star skiff , Naboo fighter |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color birth_year gender\n", "1 BB8 NA NA none none black NA none \n", "2 Captain Phasma NA NA unknown unknown unknown NA female\n", "3 Padmé Amidala 165 45 brown light brown 46 female\n", " homeworld species\n", "1 NA Droid \n", "2 NA NA \n", "3 Naboo Human \n", " films vehicles\n", "1 The Force Awakens \n", "2 The Force Awakens \n", "3 Attack of the Clones, The Phantom Menace , Revenge of the Sith \n", " starships \n", "1 \n", "2 \n", "3 H-type Nubian yacht, Naboo star skiff , Naboo fighter " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "tail(starwars, 3)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
Sly Moore 17848none pale white NAfemaleUmbara NA Attack of the Clones, Revenge of the Sith
Bail Prestor Organa191NAblacktan brown 67male AlderaanHumanAttack of the Clones, Revenge of the Sith
Ben Quadinaros 16365none grey, green, yelloworangeNAmale Tund ToongThe Phantom Menace
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t Sly Moore & 178 & 48 & none & pale & white & NA & female & Umbara & NA & Attack of the Clones, Revenge of the Sith & & \\\\\n", "\t Bail Prestor Organa & 191 & NA & black & tan & brown & 67 & male & Alderaan & Human & Attack of the Clones, Revenge of the Sith & & \\\\\n", "\t Ben Quadinaros & 163 & 65 & none & grey, green, yellow & orange & NA & male & Tund & Toong & The Phantom Menace & & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| Sly Moore | 178 | 48 | none | pale | white | NA | female | Umbara | NA | Attack of the Clones, Revenge of the Sith | | |\n", "| Bail Prestor Organa | 191 | NA | black | tan | brown | 67 | male | Alderaan | Human | Attack of the Clones, Revenge of the Sith | | |\n", "| Ben Quadinaros | 163 | 65 | none | grey, green, yellow | orange | NA | male | Tund | Toong | The Phantom Menace | | |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color\n", "1 Sly Moore 178 48 none pale white \n", "2 Bail Prestor Organa 191 NA black tan brown \n", "3 Ben Quadinaros 163 65 none grey, green, yellow orange \n", " birth_year gender homeworld species\n", "1 NA female Umbara NA \n", "2 67 male Alderaan Human \n", "3 NA male Tund Toong \n", " films vehicles starships\n", "1 Attack of the Clones, Revenge of the Sith \n", "2 Attack of the Clones, Revenge of the Sith \n", "3 The Phantom Menace " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sample_n(starwars, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Piping" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
R2-D2 96 32NA white, bluered 33.0NA Naboo DroidAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens
Darth Vader202136none white yellow41.9male TatooineHumanRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope TIE Advanced x1
Leia Organa150 49brownlight brown 19.0femaleAlderaanHumanRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens Imperial Speeder Bike
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t R2-D2 & 96 & 32 & NA & white, blue & red & 33.0 & NA & Naboo & Droid & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & & \\\\\n", "\t Darth Vader & 202 & 136 & none & white & yellow & 41.9 & male & Tatooine & Human & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope & & TIE Advanced x1\\\\\n", "\t Leia Organa & 150 & 49 & brown & light & brown & 19.0 & female & Alderaan & Human & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & Imperial Speeder Bike & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| R2-D2 | 96 | 32 | NA | white, blue | red | 33.0 | NA | Naboo | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | | |\n", "| Darth Vader | 202 | 136 | none | white | yellow | 41.9 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | | TIE Advanced x1 |\n", "| Leia Organa | 150 | 49 | brown | light | brown | 19.0 | female | Alderaan | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Imperial Speeder Bike | |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color birth_year gender\n", "1 R2-D2 96 32 NA white, blue red 33.0 NA \n", "2 Darth Vader 202 136 none white yellow 41.9 male \n", "3 Leia Organa 150 49 brown light brown 19.0 female\n", " homeworld species\n", "1 Naboo Droid \n", "2 Tatooine Human \n", "3 Alderaan Human \n", " films \n", "1 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", "2 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope \n", "3 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", " vehicles starships \n", "1 \n", "2 TIE Advanced x1\n", "3 Imperial Speeder Bike " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% head(5) %>% tail(3)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
R2-D2 96 32NA white, bluered 33.0NA Naboo DroidAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens
Darth Vader202136none white yellow41.9male TatooineHumanRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope TIE Advanced x1
Leia Organa150 49brownlight brown 19.0femaleAlderaanHumanRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens Imperial Speeder Bike
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t R2-D2 & 96 & 32 & NA & white, blue & red & 33.0 & NA & Naboo & Droid & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & & \\\\\n", "\t Darth Vader & 202 & 136 & none & white & yellow & 41.9 & male & Tatooine & Human & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope & & TIE Advanced x1\\\\\n", "\t Leia Organa & 150 & 49 & brown & light & brown & 19.0 & female & Alderaan & Human & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & Imperial Speeder Bike & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| R2-D2 | 96 | 32 | NA | white, blue | red | 33.0 | NA | Naboo | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | | |\n", "| Darth Vader | 202 | 136 | none | white | yellow | 41.9 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | | TIE Advanced x1 |\n", "| Leia Organa | 150 | 49 | brown | light | brown | 19.0 | female | Alderaan | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Imperial Speeder Bike | |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color birth_year gender\n", "1 R2-D2 96 32 NA white, blue red 33.0 NA \n", "2 Darth Vader 202 136 none white yellow 41.9 male \n", "3 Leia Organa 150 49 brown light brown 19.0 female\n", " homeworld species\n", "1 Naboo Droid \n", "2 Tatooine Human \n", "3 Alderaan Human \n", " films \n", "1 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", "2 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope \n", "3 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", " vehicles starships \n", "1 \n", "2 TIE Advanced x1\n", "3 Imperial Speeder Bike " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "head(5) %>%\n", "tail(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Select" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
namebirth_yearspecies
<chr><dbl><chr>
Luke Skywalker 19Human
C-3PO 112Droid
R2-D2 33Droid
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " name & birth\\_year & species\\\\\n", " & & \\\\\n", "\\hline\n", "\t Luke Skywalker & 19 & Human\\\\\n", "\t C-3PO & 112 & Droid\\\\\n", "\t R2-D2 & 33 & Droid\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| name <chr> | birth_year <dbl> | species <chr> |\n", "|---|---|---|\n", "| Luke Skywalker | 19 | Human |\n", "| C-3PO | 112 | Droid |\n", "| R2-D2 | 33 | Droid |\n", "\n" ], "text/plain": [ " name birth_year species\n", "1 Luke Skywalker 19 Human \n", "2 C-3PO 112 Droid \n", "3 R2-D2 33 Droid " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "select(name, birth_year, species) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
nameagespecies
<chr><dbl><chr>
Luke Skywalker 19Human
C-3PO 112Droid
R2-D2 33Droid
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " name & age & species\\\\\n", " & & \\\\\n", "\\hline\n", "\t Luke Skywalker & 19 & Human\\\\\n", "\t C-3PO & 112 & Droid\\\\\n", "\t R2-D2 & 33 & Droid\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| name <chr> | age <dbl> | species <chr> |\n", "|---|---|---|\n", "| Luke Skywalker | 19 | Human |\n", "| C-3PO | 112 | Droid |\n", "| R2-D2 | 33 | Droid |\n", "\n" ], "text/plain": [ " name age species\n", "1 Luke Skywalker 19 Human \n", "2 C-3PO 112 Droid \n", "3 R2-D2 33 Droid " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "select(name, age=birth_year, species) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 10
heightmasshair_colorskin_coloreye_colorgenderhomeworldfilmsvehiclesstarships
<int><dbl><chr><chr><chr><chr><chr><list><list><list>
17277blondfair blue maleTatooineRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens Snowspeeder , Imperial Speeder BikeX-wing , Imperial shuttle
16775NA gold yellowNA TatooineAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope
9632NA white, bluered NA Naboo Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens
\n" ], "text/latex": [ "A tibble: 3 × 10\n", "\\begin{tabular}{r|llllllllll}\n", " height & mass & hair\\_color & skin\\_color & eye\\_color & gender & homeworld & films & vehicles & starships\\\\\n", " & & & & & & & & & \\\\\n", "\\hline\n", "\t 172 & 77 & blond & fair & blue & male & Tatooine & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & Snowspeeder , Imperial Speeder Bike & X-wing , Imperial shuttle\\\\\n", "\t 167 & 75 & NA & gold & yellow & NA & Tatooine & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope & & \\\\\n", "\t 96 & 32 & NA & white, blue & red & NA & Naboo & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 10\n", "\n", "| height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | gender <chr> | homeworld <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|\n", "| 172 | 77 | blond | fair | blue | male | Tatooine | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Snowspeeder , Imperial Speeder Bike | X-wing , Imperial shuttle |\n", "| 167 | 75 | NA | gold | yellow | NA | Tatooine | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | | |\n", "| 96 | 32 | NA | white, blue | red | NA | Naboo | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | | |\n", "\n" ], "text/plain": [ " height mass hair_color skin_color eye_color gender homeworld\n", "1 172 77 blond fair blue male Tatooine \n", "2 167 75 NA gold yellow NA Tatooine \n", "3 96 32 NA white, blue red NA Naboo \n", " films \n", "1 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", "2 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope \n", "3 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", " vehicles \n", "1 Snowspeeder , Imperial Speeder Bike\n", "2 \n", "3 \n", " starships \n", "1 X-wing , Imperial shuttle\n", "2 \n", "3 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "select(-name, -birth_year, -species) %>% \n", "head(3)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
skin_colorspeciesstarships
<chr><chr><list>
fair HumanX-wing , Imperial shuttle
gold Droid
white, blueDroid
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " skin\\_color & species & starships\\\\\n", " & & \\\\\n", "\\hline\n", "\t fair & Human & X-wing , Imperial shuttle\\\\\n", "\t gold & Droid & \\\\\n", "\t white, blue & Droid & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| skin_color <chr> | species <chr> | starships <list> |\n", "|---|---|---|\n", "| fair | Human | X-wing , Imperial shuttle |\n", "| gold | Droid | |\n", "| white, blue | Droid | |\n", "\n" ], "text/plain": [ " skin_color species starships \n", "1 fair Human X-wing , Imperial shuttle\n", "2 gold Droid \n", "3 white, blue Droid " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(starts_with('s')) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 4
heighthair_colorbirth_yearspecies
<int><chr><dbl><chr>
172blond 19Human
167NA 112Droid
96NA 33Droid
\n" ], "text/latex": [ "A tibble: 3 × 4\n", "\\begin{tabular}{r|llll}\n", " height & hair\\_color & birth\\_year & species\\\\\n", " & & & \\\\\n", "\\hline\n", "\t 172 & blond & 19 & Human\\\\\n", "\t 167 & NA & 112 & Droid\\\\\n", "\t 96 & NA & 33 & Droid\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 4\n", "\n", "| height <int> | hair_color <chr> | birth_year <dbl> | species <chr> |\n", "|---|---|---|---|\n", "| 172 | blond | 19 | Human |\n", "| 167 | NA | 112 | Droid |\n", "| 96 | NA | 33 | Droid |\n", "\n" ], "text/plain": [ " height hair_color birth_year species\n", "1 172 blond 19 Human \n", "2 167 NA 112 Droid \n", "3 96 NA 33 Droid " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(matches(\"[aeiou]{2,}\")) %>%\n", "head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arrange" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
nameagemass
<chr><dbl><dbl>
Wicket Systri Warrick 8 20
IG-88 15140
Luke Skywalker 19 77
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " name & age & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t Wicket Systri Warrick & 8 & 20\\\\\n", "\t IG-88 & 15 & 140\\\\\n", "\t Luke Skywalker & 19 & 77\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| name <chr> | age <dbl> | mass <dbl> |\n", "|---|---|---|\n", "| Wicket Systri Warrick | 8 | 20 |\n", "| IG-88 | 15 | 140 |\n", "| Luke Skywalker | 19 | 77 |\n", "\n" ], "text/plain": [ " name age mass\n", "1 Wicket Systri Warrick 8 20 \n", "2 IG-88 15 140 \n", "3 Luke Skywalker 19 77 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, mass) %>%\n", "arrange(age) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
nameagemass
<chr><dbl><dbl>
Yoda 896 17
Jabba Desilijic Tiure6001358
Chewbacca 200 112
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " name & age & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t Yoda & 896 & 17\\\\\n", "\t Jabba Desilijic Tiure & 600 & 1358\\\\\n", "\t Chewbacca & 200 & 112\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| name <chr> | age <dbl> | mass <dbl> |\n", "|---|---|---|\n", "| Yoda | 896 | 17 |\n", "| Jabba Desilijic Tiure | 600 | 1358 |\n", "| Chewbacca | 200 | 112 |\n", "\n" ], "text/plain": [ " name age mass\n", "1 Yoda 896 17\n", "2 Jabba Desilijic Tiure 600 1358\n", "3 Chewbacca 200 112" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, mass) %>%\n", "arrange(desc(age)) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 5 × 3
nameagemass
<chr><dbl><dbl>
Wicket Systri Warrick 8 20
IG-88 15140
Luke Skywalker 19 77
Leia Organa 19 49
Wedge Antilles 21 77
\n" ], "text/latex": [ "A tibble: 5 × 3\n", "\\begin{tabular}{r|lll}\n", " name & age & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t Wicket Systri Warrick & 8 & 20\\\\\n", "\t IG-88 & 15 & 140\\\\\n", "\t Luke Skywalker & 19 & 77\\\\\n", "\t Leia Organa & 19 & 49\\\\\n", "\t Wedge Antilles & 21 & 77\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 3\n", "\n", "| name <chr> | age <dbl> | mass <dbl> |\n", "|---|---|---|\n", "| Wicket Systri Warrick | 8 | 20 |\n", "| IG-88 | 15 | 140 |\n", "| Luke Skywalker | 19 | 77 |\n", "| Leia Organa | 19 | 49 |\n", "| Wedge Antilles | 21 | 77 |\n", "\n" ], "text/plain": [ " name age mass\n", "1 Wicket Systri Warrick 8 20 \n", "2 IG-88 15 140 \n", "3 Luke Skywalker 19 77 \n", "4 Leia Organa 19 49 \n", "5 Wedge Antilles 21 77 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, mass) %>%\n", "arrange(age) %>%\n", "head(5)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 5 × 3
nameagemass
<chr><dbl><dbl>
Wicket Systri Warrick 8 20
IG-88 15140
Leia Organa 19 49
Luke Skywalker 19 77
Wedge Antilles 21 77
\n" ], "text/latex": [ "A tibble: 5 × 3\n", "\\begin{tabular}{r|lll}\n", " name & age & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t Wicket Systri Warrick & 8 & 20\\\\\n", "\t IG-88 & 15 & 140\\\\\n", "\t Leia Organa & 19 & 49\\\\\n", "\t Luke Skywalker & 19 & 77\\\\\n", "\t Wedge Antilles & 21 & 77\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 3\n", "\n", "| name <chr> | age <dbl> | mass <dbl> |\n", "|---|---|---|\n", "| Wicket Systri Warrick | 8 | 20 |\n", "| IG-88 | 15 | 140 |\n", "| Leia Organa | 19 | 49 |\n", "| Luke Skywalker | 19 | 77 |\n", "| Wedge Antilles | 21 | 77 |\n", "\n" ], "text/plain": [ " name age mass\n", "1 Wicket Systri Warrick 8 20 \n", "2 IG-88 15 140 \n", "3 Leia Organa 19 49 \n", "4 Luke Skywalker 19 77 \n", "5 Wedge Antilles 21 77 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, mass) %>%\n", "arrange(age, mass) %>%\n", "head(5)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
nameagemass
<chr><dbl><dbl>
Chewbacca 200 112
Jabba Desilijic Tiure6001358
Yoda 896 17
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " name & age & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t Chewbacca & 200 & 112\\\\\n", "\t Jabba Desilijic Tiure & 600 & 1358\\\\\n", "\t Yoda & 896 & 17\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| name <chr> | age <dbl> | mass <dbl> |\n", "|---|---|---|\n", "| Chewbacca | 200 | 112 |\n", "| Jabba Desilijic Tiure | 600 | 1358 |\n", "| Yoda | 896 | 17 |\n", "\n" ], "text/plain": [ " name age mass\n", "1 Chewbacca 200 112\n", "2 Jabba Desilijic Tiure 600 1358\n", "3 Yoda 896 17" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, mass) %>%\n", "top_n(age, n=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filter" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 5 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
C-3PO 167 75NA gold yellow112NA Tatooine Droid Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope
Chewbacca 228 112brownunknown blue 200male Kashyyyk Wookiee Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens AT-STMillennium Falcon, Imperial shuttle
Jabba Desilijic Tiure1751358NA green-tan, brownorange600hermaphroditeNal HuttaHutt The Phantom Menace, Return of the Jedi, A New Hope
Yoda 66 17whitegreen brown 896male NA Yoda's speciesAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back
Dooku 193 80whitefair brown 102male Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
\n" ], "text/latex": [ "A tibble: 5 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t C-3PO & 167 & 75 & NA & gold & yellow & 112 & NA & Tatooine & Droid & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope & & \\\\\n", "\t Chewbacca & 228 & 112 & brown & unknown & blue & 200 & male & Kashyyyk & Wookiee & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & AT-ST & Millennium Falcon, Imperial shuttle \\\\\n", "\t Jabba Desilijic Tiure & 175 & 1358 & NA & green-tan, brown & orange & 600 & hermaphrodite & Nal Hutta & Hutt & The Phantom Menace, Return of the Jedi, A New Hope & & \\\\\n", "\t Yoda & 66 & 17 & white & green & brown & 896 & male & NA & Yoda's species & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back & & \\\\\n", "\t Dooku & 193 & 80 & white & fair & brown & 102 & male & Serenno & Human & Attack of the Clones, Revenge of the Sith & Flitknot speeder & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| C-3PO | 167 | 75 | NA | gold | yellow | 112 | NA | Tatooine | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | | |\n", "| Chewbacca | 228 | 112 | brown | unknown | blue | 200 | male | Kashyyyk | Wookiee | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | AT-ST | Millennium Falcon, Imperial shuttle |\n", "| Jabba Desilijic Tiure | 175 | 1358 | NA | green-tan, brown | orange | 600 | hermaphrodite | Nal Hutta | Hutt | The Phantom Menace, Return of the Jedi, A New Hope | | |\n", "| Yoda | 66 | 17 | white | green | brown | 896 | male | NA | Yoda's species | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back | | |\n", "| Dooku | 193 | 80 | white | fair | brown | 102 | male | Serenno | Human | Attack of the Clones, Revenge of the Sith | Flitknot speeder | |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color\n", "1 C-3PO 167 75 NA gold yellow \n", "2 Chewbacca 228 112 brown unknown blue \n", "3 Jabba Desilijic Tiure 175 1358 NA green-tan, brown orange \n", "4 Yoda 66 17 white green brown \n", "5 Dooku 193 80 white fair brown \n", " birth_year gender homeworld species \n", "1 112 NA Tatooine Droid \n", "2 200 male Kashyyyk Wookiee \n", "3 600 hermaphrodite Nal Hutta Hutt \n", "4 896 male NA Yoda's species\n", "5 102 male Serenno Human \n", " films \n", "1 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope \n", "2 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", "3 The Phantom Menace, Return of the Jedi, A New Hope \n", "4 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back \n", "5 Attack of the Clones, Revenge of the Sith \n", " vehicles starships \n", "1 \n", "2 AT-ST Millennium Falcon, Imperial shuttle \n", "3 \n", "4 \n", "5 Flitknot speeder " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "filter(birth_year > 100)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
Jabba Desilijic Tiure1751358NA green-tan, brownorange600hermaphroditeNal HuttaHutt The Phantom Menace, Return of the Jedi, A New Hope
Yoda 66 17whitegreen brown 896male NA Yoda's speciesAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back
Dooku 193 80whitefair brown 102male Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t Jabba Desilijic Tiure & 175 & 1358 & NA & green-tan, brown & orange & 600 & hermaphrodite & Nal Hutta & Hutt & The Phantom Menace, Return of the Jedi, A New Hope & & \\\\\n", "\t Yoda & 66 & 17 & white & green & brown & 896 & male & NA & Yoda's species & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back & & \\\\\n", "\t Dooku & 193 & 80 & white & fair & brown & 102 & male & Serenno & Human & Attack of the Clones, Revenge of the Sith & Flitknot speeder & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| Jabba Desilijic Tiure | 175 | 1358 | NA | green-tan, brown | orange | 600 | hermaphrodite | Nal Hutta | Hutt | The Phantom Menace, Return of the Jedi, A New Hope | | |\n", "| Yoda | 66 | 17 | white | green | brown | 896 | male | NA | Yoda's species | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back | | |\n", "| Dooku | 193 | 80 | white | fair | brown | 102 | male | Serenno | Human | Attack of the Clones, Revenge of the Sith | Flitknot speeder | |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color\n", "1 Jabba Desilijic Tiure 175 1358 NA green-tan, brown orange \n", "2 Yoda 66 17 white green brown \n", "3 Dooku 193 80 white fair brown \n", " birth_year gender homeworld species \n", "1 600 hermaphrodite Nal Hutta Hutt \n", "2 896 male NA Yoda's species\n", "3 102 male Serenno Human \n", " films \n", "1 The Phantom Menace, Return of the Jedi, A New Hope \n", "2 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back\n", "3 Attack of the Clones, Revenge of the Sith \n", " vehicles starships\n", "1 \n", "2 \n", "3 Flitknot speeder " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "filter((birth_year >= 600) | (name == 'Dooku'))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
nameagehomeworld
<chr><dbl><chr>
Jabba Desilijic Tiure600Nal Hutta
Yoda 896NA
Dooku 102Serenno
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " name & age & homeworld\\\\\n", " & & \\\\\n", "\\hline\n", "\t Jabba Desilijic Tiure & 600 & Nal Hutta\\\\\n", "\t Yoda & 896 & NA \\\\\n", "\t Dooku & 102 & Serenno \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| name <chr> | age <dbl> | homeworld <chr> |\n", "|---|---|---|\n", "| Jabba Desilijic Tiure | 600 | Nal Hutta |\n", "| Yoda | 896 | NA |\n", "| Dooku | 102 | Serenno |\n", "\n" ], "text/plain": [ " name age homeworld\n", "1 Jabba Desilijic Tiure 600 Nal Hutta\n", "2 Yoda 896 NA \n", "3 Dooku 102 Serenno " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "filter((birth_year >= 600) | (name == 'Dooku')) %>%\n", "select(name, age=birth_year, homeworld)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
Chewbacca228112brownunknownblue 200maleKashyyykWookiee Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens AT-STMillennium Falcon, Imperial shuttle
Yoda 66 17whitegreen brown896maleNA Yoda's speciesAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back
Dooku 193 80whitefair brown102maleSerenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t Chewbacca & 228 & 112 & brown & unknown & blue & 200 & male & Kashyyyk & Wookiee & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & AT-ST & Millennium Falcon, Imperial shuttle \\\\\n", "\t Yoda & 66 & 17 & white & green & brown & 896 & male & NA & Yoda's species & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back & & \\\\\n", "\t Dooku & 193 & 80 & white & fair & brown & 102 & male & Serenno & Human & Attack of the Clones, Revenge of the Sith & Flitknot speeder & \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| Chewbacca | 228 | 112 | brown | unknown | blue | 200 | male | Kashyyyk | Wookiee | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | AT-ST | Millennium Falcon, Imperial shuttle |\n", "| Yoda | 66 | 17 | white | green | brown | 896 | male | NA | Yoda's species | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back | | |\n", "| Dooku | 193 | 80 | white | fair | brown | 102 | male | Serenno | Human | Attack of the Clones, Revenge of the Sith | Flitknot speeder | |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color birth_year gender\n", "1 Chewbacca 228 112 brown unknown blue 200 male \n", "2 Yoda 66 17 white green brown 896 male \n", "3 Dooku 193 80 white fair brown 102 male \n", " homeworld species \n", "1 Kashyyyk Wookiee \n", "2 NA Yoda's species\n", "3 Serenno Human \n", " films \n", "1 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", "2 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back\n", "3 Attack of the Clones, Revenge of the Sith \n", " vehicles starships \n", "1 AT-ST Millennium Falcon, Imperial shuttle \n", "2 \n", "3 Flitknot speeder " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "filter(birth_year > 100, gender=='male')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 13
nameheightmasshair_colorskin_coloreye_colorbirth_yeargenderhomeworldspeciesfilmsvehiclesstarships
<chr><int><dbl><chr><chr><chr><dbl><chr><chr><chr><list><list><list>
Luke Skywalker172 77blondfair blue 19.0maleTatooineHumanRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens Snowspeeder , Imperial Speeder BikeX-wing , Imperial shuttle
C-3PO 167 75NA gold yellow112.0NA TatooineDroidAttack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope
Darth Vader 202136none whiteyellow 41.9maleTatooineHumanRevenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope TIE Advanced x1
\n" ], "text/latex": [ "A tibble: 3 × 13\n", "\\begin{tabular}{r|lllllllllllll}\n", " name & height & mass & hair\\_color & skin\\_color & eye\\_color & birth\\_year & gender & homeworld & species & films & vehicles & starships\\\\\n", " & & & & & & & & & & & & \\\\\n", "\\hline\n", "\t Luke Skywalker & 172 & 77 & blond & fair & blue & 19.0 & male & Tatooine & Human & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens & Snowspeeder , Imperial Speeder Bike & X-wing , Imperial shuttle\\\\\n", "\t C-3PO & 167 & 75 & NA & gold & yellow & 112.0 & NA & Tatooine & Droid & Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope & & \\\\\n", "\t Darth Vader & 202 & 136 & none & white & yellow & 41.9 & male & Tatooine & Human & Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope & & TIE Advanced x1\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 13\n", "\n", "| name <chr> | height <int> | mass <dbl> | hair_color <chr> | skin_color <chr> | eye_color <chr> | birth_year <dbl> | gender <chr> | homeworld <chr> | species <chr> | films <list> | vehicles <list> | starships <list> |\n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| Luke Skywalker | 172 | 77 | blond | fair | blue | 19.0 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens | Snowspeeder , Imperial Speeder Bike | X-wing , Imperial shuttle |\n", "| C-3PO | 167 | 75 | NA | gold | yellow | 112.0 | NA | Tatooine | Droid | Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | | |\n", "| Darth Vader | 202 | 136 | none | white | yellow | 41.9 | male | Tatooine | Human | Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope | | TIE Advanced x1 |\n", "\n" ], "text/plain": [ " name height mass hair_color skin_color eye_color birth_year gender\n", "1 Luke Skywalker 172 77 blond fair blue 19.0 male \n", "2 C-3PO 167 75 NA gold yellow 112.0 NA \n", "3 Darth Vader 202 136 none white yellow 41.9 male \n", " homeworld species\n", "1 Tatooine Human \n", "2 Tatooine Droid \n", "3 Tatooine Human \n", " films \n", "1 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope , The Force Awakens \n", "2 Attack of the Clones , The Phantom Menace , Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope \n", "3 Revenge of the Sith , Return of the Jedi , The Empire Strikes Back, A New Hope \n", " vehicles \n", "1 Snowspeeder , Imperial Speeder Bike\n", "2 \n", "3 \n", " starships \n", "1 X-wing , Imperial shuttle\n", "2 \n", "3 TIE Advanced x1 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "filter(str_detect(homeworld, \"in\")) %>%\n", "head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mutate" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 4
nameageheightmass
<chr><dbl><int><dbl>
Luke Skywalker 1917277
C-3PO 11216775
R2-D2 33 9632
\n" ], "text/latex": [ "A tibble: 3 × 4\n", "\\begin{tabular}{r|llll}\n", " name & age & height & mass\\\\\n", " & & & \\\\\n", "\\hline\n", "\t Luke Skywalker & 19 & 172 & 77\\\\\n", "\t C-3PO & 112 & 167 & 75\\\\\n", "\t R2-D2 & 33 & 96 & 32\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 4\n", "\n", "| name <chr> | age <dbl> | height <int> | mass <dbl> |\n", "|---|---|---|---|\n", "| Luke Skywalker | 19 | 172 | 77 |\n", "| C-3PO | 112 | 167 | 75 |\n", "| R2-D2 | 33 | 96 | 32 |\n", "\n" ], "text/plain": [ " name age height mass\n", "1 Luke Skywalker 19 172 77 \n", "2 C-3PO 112 167 75 \n", "3 R2-D2 33 96 32 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, height, mass) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 6
nameageheightmassbmiobese
<chr><dbl><int><dbl><dbl><lgl>
Luke Skywalker 191727726.02758FALSE
C-3PO 1121677526.89232FALSE
R2-D2 33 963234.72222 TRUE
\n" ], "text/latex": [ "A tibble: 3 × 6\n", "\\begin{tabular}{r|llllll}\n", " name & age & height & mass & bmi & obese\\\\\n", " & & & & & \\\\\n", "\\hline\n", "\t Luke Skywalker & 19 & 172 & 77 & 26.02758 & FALSE\\\\\n", "\t C-3PO & 112 & 167 & 75 & 26.89232 & FALSE\\\\\n", "\t R2-D2 & 33 & 96 & 32 & 34.72222 & TRUE\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 6\n", "\n", "| name <chr> | age <dbl> | height <int> | mass <dbl> | bmi <dbl> | obese <lgl> |\n", "|---|---|---|---|---|---|\n", "| Luke Skywalker | 19 | 172 | 77 | 26.02758 | FALSE |\n", "| C-3PO | 112 | 167 | 75 | 26.89232 | FALSE |\n", "| R2-D2 | 33 | 96 | 32 | 34.72222 | TRUE |\n", "\n" ], "text/plain": [ " name age height mass bmi obese\n", "1 Luke Skywalker 19 172 77 26.02758 FALSE\n", "2 C-3PO 112 167 75 26.89232 FALSE\n", "3 R2-D2 33 96 32 34.72222 TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, height, mass) %>%\n", "mutate(bmi=mass/(height/100)^2, obese=bmi>30) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 2
bmiobese
<dbl><lgl>
26.02758FALSE
26.89232FALSE
34.72222 TRUE
\n" ], "text/latex": [ "A tibble: 3 × 2\n", "\\begin{tabular}{r|ll}\n", " bmi & obese\\\\\n", " & \\\\\n", "\\hline\n", "\t 26.02758 & FALSE\\\\\n", "\t 26.89232 & FALSE\\\\\n", "\t 34.72222 & TRUE\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 2\n", "\n", "| bmi <dbl> | obese <lgl> |\n", "|---|---|\n", "| 26.02758 | FALSE |\n", "| 26.89232 | FALSE |\n", "| 34.72222 | TRUE |\n", "\n" ], "text/plain": [ " bmi obese\n", "1 26.02758 FALSE\n", "2 26.89232 FALSE\n", "3 34.72222 TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars%>% \n", "select(name, age=birth_year, height, mass) %>%\n", "transmute(bmi=mass/(height/100)^2, obese=bmi>30) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 7
namehair_colorskin_coloreye_colorgenderhomeworldspecies
<chr><chr><chr><chr><chr><chr><chr>
LUKE SKYWALKERBLONDFAIR BLUE MALETATOOINEHUMAN
C-3PO NA GOLD YELLOWNA TATOOINEDROID
R2-D2 NA WHITE, BLUERED NA NABOO DROID
\n" ], "text/latex": [ "A tibble: 3 × 7\n", "\\begin{tabular}{r|lllllll}\n", " name & hair\\_color & skin\\_color & eye\\_color & gender & homeworld & species\\\\\n", " & & & & & & \\\\\n", "\\hline\n", "\t LUKE SKYWALKER & BLOND & FAIR & BLUE & MALE & TATOOINE & HUMAN\\\\\n", "\t C-3PO & NA & GOLD & YELLOW & NA & TATOOINE & DROID\\\\\n", "\t R2-D2 & NA & WHITE, BLUE & RED & NA & NABOO & DROID\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 7\n", "\n", "| name <chr> | hair_color <chr> | skin_color <chr> | eye_color <chr> | gender <chr> | homeworld <chr> | species <chr> |\n", "|---|---|---|---|---|---|---|\n", "| LUKE SKYWALKER | BLOND | FAIR | BLUE | MALE | TATOOINE | HUMAN |\n", "| C-3PO | NA | GOLD | YELLOW | NA | TATOOINE | DROID |\n", "| R2-D2 | NA | WHITE, BLUE | RED | NA | NABOO | DROID |\n", "\n" ], "text/plain": [ " name hair_color skin_color eye_color gender homeworld species\n", "1 LUKE SKYWALKER BLOND FAIR BLUE MALE TATOOINE HUMAN \n", "2 C-3PO NA GOLD YELLOW NA TATOOINE DROID \n", "3 R2-D2 NA WHITE, BLUE RED NA NABOO DROID " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "transmute_if(is_character, str_to_upper) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
birth_yearheightmass
<dbl><int><dbl>
1917277
11216775
33 9632
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " birth\\_year & height & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t 19 & 172 & 77\\\\\n", "\t 112 & 167 & 75\\\\\n", "\t 33 & 96 & 32\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| birth_year <dbl> | height <int> | mass <dbl> |\n", "|---|---|---|\n", "| 19 | 172 | 77 |\n", "| 112 | 167 | 75 |\n", "| 33 | 96 | 32 |\n", "\n" ], "text/plain": [ " birth_year height mass\n", "1 19 172 77 \n", "2 112 167 75 \n", "3 33 96 32 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "select_at(c(\"birth_year\", \"height\", \"mass\")) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 3
birth_yearheightmass
<dbl><dbl><dbl>
2017378
11316876
34 9733
\n" ], "text/latex": [ "A tibble: 3 × 3\n", "\\begin{tabular}{r|lll}\n", " birth\\_year & height & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t 20 & 173 & 78\\\\\n", "\t 113 & 168 & 76\\\\\n", "\t 34 & 97 & 33\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 3\n", "\n", "| birth_year <dbl> | height <dbl> | mass <dbl> |\n", "|---|---|---|\n", "| 20 | 173 | 78 |\n", "| 113 | 168 | 76 |\n", "| 34 | 97 | 33 |\n", "\n" ], "text/plain": [ " birth_year height mass\n", "1 20 173 78 \n", "2 113 168 76 \n", "3 34 97 33 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "transmute_at(c(\"birth_year\", \"height\", \"mass\"), ~ . + 1) %>%\n", "head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summarize" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\n", "
A tibble: 1 × 2
avg_massmedian_age
<dbl><dbl>
97.3118652
\n" ], "text/latex": [ "A tibble: 1 × 2\n", "\\begin{tabular}{r|ll}\n", " avg\\_mass & median\\_age\\\\\n", " & \\\\\n", "\\hline\n", "\t 97.31186 & 52\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 1 × 2\n", "\n", "| avg_mass <dbl> | median_age <dbl> |\n", "|---|---|\n", "| 97.31186 | 52 |\n", "\n" ], "text/plain": [ " avg_mass median_age\n", "1 97.31186 52 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "summarize(avg_mass = mean(mass, na.rm=TRUE), \n", " median_age=median(birth_year, na.rm=TRUE))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\n", "
A tibble: 1 × 3
heightmassbirth_year
<dbl><dbl><dbl>
174.35897.3118687.56512
\n" ], "text/latex": [ "A tibble: 1 × 3\n", "\\begin{tabular}{r|lll}\n", " height & mass & birth\\_year\\\\\n", " & & \\\\\n", "\\hline\n", "\t 174.358 & 97.31186 & 87.56512\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 1 × 3\n", "\n", "| height <dbl> | mass <dbl> | birth_year <dbl> |\n", "|---|---|---|\n", "| 174.358 | 97.31186 | 87.56512 |\n", "\n" ], "text/plain": [ " height mass birth_year\n", "1 174.358 97.31186 87.56512 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "summarize_if(is.numeric, mean, na.rm=TRUE)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\n", "
A tibble: 1 × 6
height_fn1mass_fn1birth_year_fn1height_fn2mass_fn2birth_year_fn2
<dbl><dbl><dbl><int><dbl><dbl>
174.35897.3118687.565121807952
\n" ], "text/latex": [ "A tibble: 1 × 6\n", "\\begin{tabular}{r|llllll}\n", " height\\_fn1 & mass\\_fn1 & birth\\_year\\_fn1 & height\\_fn2 & mass\\_fn2 & birth\\_year\\_fn2\\\\\n", " & & & & & \\\\\n", "\\hline\n", "\t 174.358 & 97.31186 & 87.56512 & 180 & 79 & 52\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 1 × 6\n", "\n", "| height_fn1 <dbl> | mass_fn1 <dbl> | birth_year_fn1 <dbl> | height_fn2 <int> | mass_fn2 <dbl> | birth_year_fn2 <dbl> |\n", "|---|---|---|---|---|---|\n", "| 174.358 | 97.31186 | 87.56512 | 180 | 79 | 52 |\n", "\n" ], "text/plain": [ " height_fn1 mass_fn1 birth_year_fn1 height_fn2 mass_fn2 birth_year_fn2\n", "1 174.358 97.31186 87.56512 180 79 52 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "summarize_if(is.numeric, list(mean, median), na.rm=TRUE)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Group by" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 2
homeworldavg_mass
<chr><dbl>
NA 82
Alderaan 64
Aleen Minor15
\n" ], "text/latex": [ "A tibble: 3 × 2\n", "\\begin{tabular}{r|ll}\n", " homeworld & avg\\_mass\\\\\n", " & \\\\\n", "\\hline\n", "\t NA & 82\\\\\n", "\t Alderaan & 64\\\\\n", "\t Aleen Minor & 15\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 2\n", "\n", "| homeworld <chr> | avg_mass <dbl> |\n", "|---|---|\n", "| NA | 82 |\n", "| Alderaan | 64 |\n", "| Aleen Minor | 15 |\n", "\n" ], "text/plain": [ " homeworld avg_mass\n", "1 NA 82 \n", "2 Alderaan 64 \n", "3 Aleen Minor 15 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "group_by(homeworld) %>% \n", "summarize(avg_mass=mean(mass, na.rm=T)) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 3 × 2
homeworldavg_mass
<chr><dbl>
Alderaan 64
Aleen Minor15
Bespin 79
\n" ], "text/latex": [ "A tibble: 3 × 2\n", "\\begin{tabular}{r|ll}\n", " homeworld & avg\\_mass\\\\\n", " & \\\\\n", "\\hline\n", "\t Alderaan & 64\\\\\n", "\t Aleen Minor & 15\\\\\n", "\t Bespin & 79\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 3 × 2\n", "\n", "| homeworld <chr> | avg_mass <dbl> |\n", "|---|---|\n", "| Alderaan | 64 |\n", "| Aleen Minor | 15 |\n", "| Bespin | 79 |\n", "\n" ], "text/plain": [ " homeworld avg_mass\n", "1 Alderaan 64 \n", "2 Aleen Minor 15 \n", "3 Bespin 79 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "group_by(homeworld) %>% \n", "summarize(avg_mass=mean(mass, na.rm=T)) %>%\n", "filter(!is.na(homeworld)) %>%\n", "head(3)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A grouped_df: 5 × 5
homeworldspeciesgenderavg_massn
<chr><chr><chr><dbl><int>
Alderaan Human female 491
Alderaan Human male 792
Aleen MinorAleenamale 151
Bespin Human male 791
Bestine IV Human male 1101
\n" ], "text/latex": [ "A grouped_df: 5 × 5\n", "\\begin{tabular}{r|lllll}\n", " homeworld & species & gender & avg\\_mass & n\\\\\n", " & & & & \\\\\n", "\\hline\n", "\t Alderaan & Human & female & 49 & 1\\\\\n", "\t Alderaan & Human & male & 79 & 2\\\\\n", "\t Aleen Minor & Aleena & male & 15 & 1\\\\\n", "\t Bespin & Human & male & 79 & 1\\\\\n", "\t Bestine IV & Human & male & 110 & 1\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A grouped_df: 5 × 5\n", "\n", "| homeworld <chr> | species <chr> | gender <chr> | avg_mass <dbl> | n <int> |\n", "|---|---|---|---|---|\n", "| Alderaan | Human | female | 49 | 1 |\n", "| Alderaan | Human | male | 79 | 2 |\n", "| Aleen Minor | Aleena | male | 15 | 1 |\n", "| Bespin | Human | male | 79 | 1 |\n", "| Bestine IV | Human | male | 110 | 1 |\n", "\n" ], "text/plain": [ " homeworld species gender avg_mass n\n", "1 Alderaan Human female 49 1\n", "2 Alderaan Human male 79 2\n", "3 Aleen Minor Aleena male 15 1\n", "4 Bespin Human male 79 1\n", "5 Bestine IV Human male 110 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "group_by(homeworld, species, gender) %>% \n", "summarize(avg_mass=mean(mass, na.rm=T), n=n()) %>%\n", "filter(!is.na(homeworld)) %>%\n", "head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1**. Show the name, height and mass of all Droids sorted by height from tallest to shortest." ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 5 × 3
nameheightmass
<chr><int><dbl>
IG-88200140
C-3PO167 75
R5-D4 97 32
R2-D2 96 32
BB8 NA NA
\n" ], "text/latex": [ "A tibble: 5 × 3\n", "\\begin{tabular}{r|lll}\n", " name & height & mass\\\\\n", " & & \\\\\n", "\\hline\n", "\t IG-88 & 200 & 140\\\\\n", "\t C-3PO & 167 & 75\\\\\n", "\t R5-D4 & 97 & 32\\\\\n", "\t R2-D2 & 96 & 32\\\\\n", "\t BB8 & NA & NA\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 3\n", "\n", "| name <chr> | height <int> | mass <dbl> |\n", "|---|---|---|\n", "| IG-88 | 200 | 140 |\n", "| C-3PO | 167 | 75 |\n", "| R5-D4 | 97 | 32 |\n", "| R2-D2 | 96 | 32 |\n", "| BB8 | NA | NA |\n", "\n" ], "text/plain": [ " name height mass\n", "1 IG-88 200 140 \n", "2 C-3PO 167 75 \n", "3 R5-D4 97 32 \n", "4 R2-D2 96 32 \n", "5 BB8 NA NA " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>%\n", "filter(species == 'Droid') %>%\n", "arrange(desc(height)) %>%\n", "select(name, height, mass)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2**. List the name and hoemworld of all non-humans who appeared in `The Empire Strikes Back`." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning message in stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)):\n", "“argument is not an atomic vector; coercing”" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
A tibble: 6 × 2
namehomeworld
<chr><chr>
C-3PO Tatooine
R2-D2 Naboo
ChewbaccaKashyyyk
Yoda NA
IG-88 NA
Bossk Trandosha
\n" ], "text/latex": [ "A tibble: 6 × 2\n", "\\begin{tabular}{r|ll}\n", " name & homeworld\\\\\n", " & \\\\\n", "\\hline\n", "\t C-3PO & Tatooine \\\\\n", "\t R2-D2 & Naboo \\\\\n", "\t Chewbacca & Kashyyyk \\\\\n", "\t Yoda & NA \\\\\n", "\t IG-88 & NA \\\\\n", "\t Bossk & Trandosha\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 6 × 2\n", "\n", "| name <chr> | homeworld <chr> |\n", "|---|---|\n", "| C-3PO | Tatooine |\n", "| R2-D2 | Naboo |\n", "| Chewbacca | Kashyyyk |\n", "| Yoda | NA |\n", "| IG-88 | NA |\n", "| Bossk | Trandosha |\n", "\n" ], "text/plain": [ " name homeworld\n", "1 C-3PO Tatooine \n", "2 R2-D2 Naboo \n", "3 Chewbacca Kashyyyk \n", "4 Yoda NA \n", "5 IG-88 NA \n", "6 Bossk Trandosha" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "filter(str_detect(films, 'The Empire Strikes Back'), species!='Human') %>%\n", "select(name, homeworld)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3**. Which homeworld has the most humans?" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Selecting by n\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\n", "
A tibble: 1 × 2
homeworldn
<chr><int>
Tatooine8
\n" ], "text/latex": [ "A tibble: 1 × 2\n", "\\begin{tabular}{r|ll}\n", " homeworld & n\\\\\n", " & \\\\\n", "\\hline\n", "\t Tatooine & 8\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 1 × 2\n", "\n", "| homeworld <chr> | n <int> |\n", "|---|---|\n", "| Tatooine | 8 |\n", "\n" ], "text/plain": [ " homeworld n\n", "1 Tatooine 8" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "filter(species == \"Human\") %>%\n", "group_by(homeworld) %>% \n", "summarize(n=n()) %>%\n", "top_n(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4**. What species has the highest average BMI? Exclude Species with only 1 member, and ignore NAs in the calculation of the average." ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\n", "
A tibble: 1 × 3
speciesavg_bmin
<chr><dbl><int>
Droid32.656135
\n" ], "text/latex": [ "A tibble: 1 × 3\n", "\\begin{tabular}{r|lll}\n", " species & avg\\_bmi & n\\\\\n", " & & \\\\\n", "\\hline\n", "\t Droid & 32.65613 & 5\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 1 × 3\n", "\n", "| species <chr> | avg_bmi <dbl> | n <int> |\n", "|---|---|---|\n", "| Droid | 32.65613 | 5 |\n", "\n" ], "text/plain": [ " species avg_bmi n\n", "1 Droid 32.65613 5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "starwars %>% \n", "mutate(bmi = mass/(height/100)^2) %>%\n", "group_by(species) %>%\n", "summarize(avg_bmi=mean(bmi, na.rm=T), n=n()) %>%\n", "filter(n>1) %>%\n", "top_n(avg_bmi, n=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }