name: module1 class: title-slide, right, middle, hide-count, hide-logo background-image: url("https://images.unsplash.com/photo-1513949474464-08a0bdb4fa54?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1770&q=80") background-size: cover # .white.big-text[R and<br>RStudio:<br>Basics] ## .white.black[Session - 1] .footnote[ .white[Image credits:][Kelly Sikkema ](https://unsplash.com/photos/p1ZgObLpa0A) ] --- class: middle .pull-left[ <img src="images/ajaykoli.png" width="70%" style="display: block; margin: auto auto auto 0;" /> Dr Ajay Kumar Koli<br>Head of School of Data Science<br><svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M320 336c0 8.84-7.16 16-16 16h-96c-8.84 0-16-7.16-16-16v-48H0v144c0 25.6 22.4 48 48 48h416c25.6 0 48-22.4 48-48V288H320v48zm144-208h-80V80c0-25.6-22.4-48-48-48H176c-25.6 0-48 22.4-48 48v48H48c-25.6 0-48 22.4-48 48v80h512v-80c0-25.6-22.4-48-48-48zm-144 0H192V96h128v32z"></path></svg> [Nalanda Academy](https://nalanda-academy.org/) - Wardha<br><svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> [@ajay_kolii](https://twitter.com/ajay_kolii)<br><svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm0 48v40.805c-22.422 18.259-58.168 46.651-134.587 106.49-16.841 13.247-50.201 45.072-73.413 44.701-23.208.375-56.579-31.459-73.413-44.701C106.18 199.465 70.425 171.067 48 152.805V112h416zM48 400V214.398c22.914 18.251 55.409 43.862 104.938 82.646 21.857 17.205 60.134 55.186 103.062 54.955 42.717.231 80.509-37.199 103.053-54.947 49.528-38.783 82.032-64.401 104.947-82.653V400H48z"></path></svg> [koliajaykumar@gmail.com](koliajaykumar@gmail.com) <br><svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M336.5 160C322 70.7 287.8 8 248 8s-74 62.7-88.5 152h177zM152 256c0 22.2 1.2 43.5 3.3 64h185.3c2.1-20.5 3.3-41.8 3.3-64s-1.2-43.5-3.3-64H155.3c-2.1 20.5-3.3 41.8-3.3 64zm324.7-96c-28.6-67.9-86.5-120.4-158-141.6 24.4 33.8 41.2 84.7 50 141.6h108zM177.2 18.4C105.8 39.6 47.8 92.1 19.3 160h108c8.7-56.9 25.5-107.8 49.9-141.6zM487.4 192H372.7c2.1 21 3.3 42.5 3.3 64s-1.2 43-3.3 64h114.6c5.5-20.5 8.6-41.8 8.6-64s-3.1-43.5-8.5-64zM120 256c0-21.5 1.2-43 3.3-64H8.6C3.2 212.5 0 233.8 0 256s3.2 43.5 8.6 64h114.6c-2-21-3.2-42.5-3.2-64zm39.5 96c14.5 89.3 48.7 152 88.5 152s74-62.7 88.5-152h-177zm159.3 141.6c71.4-21.2 129.4-73.7 158-141.6h-108c-8.8 56.9-25.6 107.8-50 141.6zM19.3 352c28.6 67.9 86.5 120.4 158 141.6-24.4-33.8-41.2-84.7-50-141.6h-108z"></path></svg> [https://koliajay.netlify.app/](https://koliajay.netlify.app/) ] .pull-right[ <br> <br> <br> <br> # .big-text[Hello! 😊] ] --- # Modules: 1. .b[Basics of R & RStudio] 1. Dynamic Documents using R Markdown 1. Data Visualisation using ggplot2 1. Data Wrangling using dplyr 1. Slide Crafting using xaringan --- background-image: url("images/r_first_then.png") background-size: contain .footnote[ [Source](https://github.com/allisonhorst/stats-illustrations) ] --- # 😍 R is FREE - R is a language and environment for statistical computing and graphics. (R project) -- - In August 1993, designed by .pull-left[ <div class="figure" style="text-align: center"> <img src="https://cdn.auckland.ac.nz/aem/content/auckland/en/science/about-the-faculty/department-of-statistics/ihaka-lecture-series/_jcr_content/leftpar/imagecomponent/image.img.480.low.jpg/1561079330278.jpg" alt="Ross Ihaka <br>(New Zealand Statistician)" width="80%" /> <p class="caption">Ross Ihaka <br>(New Zealand Statistician)</p> </div> ] .pull-right[ <div class="figure" style="text-align: center"> <img src="https://www.computerhope.com/people/pictures/robert_gentleman.jpg" alt="Robert Gentleman<br>(Canadian Statistician)" width="50%" /> <p class="caption">Robert Gentleman<br>(Canadian Statistician)</p> </div> ] --- class: center # Download R from [CRAN](https://cran.r-project.org/mirrors.html) <img src="images/cran.png" width="80%" style="display: block; margin: auto;" /> --- class: right .pull-left[ # R Console ##- R version ##- R name ##- R licence ##- prompt `>` ] .pull-right[ <img src="images/console.png" width="95%" style="display: block; margin: auto auto auto 0;" /> ] --- # .red[Never Save R "Workspace Image":] .pull-left[ - It helps in "freshly minted R sessions". - "put more trust in your script than in your memory" .footnote[ [Source](https://www.r-bloggers.com/2017/04/using-r-dont-save-your-workspace/) ] ] .pull-right[ <img src="images/quit.png" width="75%" style="display: block; margin: auto;" /> ] --- # .center[R as a BIG calculator] .pull-left[ ### .center[What you code] ```r *1 ``` ] .pull-right[ ### .center[What you see] ``` ## [1] 1 ``` ] --- # .center[R as a BIG calculator] .pull-left[ ### .center[What you code] ```r 1 *1 + 1 ``` ] .pull-right[ ### .center[What you see] ``` ## [1] 1 ## [1] 2 ``` ] --- # .center[R as a BIG calculator] .pull-left[ ### .center[What you code] ```r 1 1 + 1 *34 / 40 ``` ] .pull-right[ ### .center[What you see] ``` ## [1] 1 ## [1] 2 ## [1] 0.85 ``` ] --- # .center[R as a BIG calculator] .pull-left[ ### .center[What you code] ```r 1 1 + 1 34 / 40 *5 < 4 ``` ] .pull-right[ ### .center[What you see] ``` ## [1] 1 ## [1] 2 ## [1] 0.85 ## [1] FALSE ``` ] --- # .center[R as a BIG calculator] .pull-left[ ### .center[What you code] ```r 1 1 + 1 34 / 40 5 < 4 *16 == 16 ``` ] .pull-right[ ### .center[What you see] ``` ## [1] 1 ## [1] 2 ## [1] 0.85 ## [1] FALSE ## [1] TRUE ``` ] --- # .center[Plot using R] .panelset[ .panel[.panel-name[Code] ```r *plot(1:100) ``` ] .panel[.panel-name[Output] <img src="rstudio_files/figure-html/unnamed-chunk-7-1.png" width="40%" style="display: block; margin: auto;" /> ] ] --- class: inverse, hide-count, center, bottom, hide-logo background-image: url("images/rainbowr.gif") background-size: 50% background-position: 50% 5% # .bottom.big-text[Functions] --- # R Function - "A function, in a programming environment, is a set of instructions. A programmer builds a function to avoid repeating the same task, or reduce complexity." <br> -- .panelset[ .panel[.panel-name[Code] ```r *round(9.1565, 2) ``` ] .panel[.panel-name[Output] ``` ## [1] 9.16 ``` ] ] --- class: center background-image: url("images/function-args.png") background-size: 90% background-position: 60% 70% # Structure of R function --- # .center[Round Function] .panelset[ .panel[.panel-name[Code] ```r round(x = 564.56743, digits = 2) ``` ] .panel[.panel-name[Output] ``` ## [1] 564.57 ``` ] ] --- # .center[Round Function] .panelset[ .panel[.panel-name[Code] ```r round(x = 564.56743, digits = 1) ``` ] .panel[.panel-name[Output] ``` ## [1] 564.6 ``` ] ] --- # .center[Square Root Function] .panelset[ .panel[.panel-name[Code] ```r *sqrt(x = 9) ``` ] .panel[.panel-name[Output] ``` ## [1] 3 ``` ] ] --- # .center[Sequence Function] .panelset[ .panel[.panel-name[Code] ```r *seq.int(from = 10, to = 30, by = 5) ``` ] .panel[.panel-name[Output] ``` ## [1] 10 15 20 25 30 ``` ] ] --- class: inverse, hide-count, center, bottom, hide-logo background-image: url("images/stormyr.gif") background-size: 42% background-position: 50% 5% # .bottom.big-text[Comments] --- # R Comment: - "Humans will be able to read the comments, but your computer will pass over them."<sup>1</sup> -- - In R, `#` is used as a commenting symbol .footnote[ [Source](https://en.wikipedia.org/wiki/Comment_(computer_programming) ] --- # .center[How to add comment?] .panelset[ .panel[.panel-name[Code] ```r # secret code of happiness (1 + 4) - (3 * 5) / 10 ## r does not evaluate this # all these are comments # comments are very important ``` ] .panel[.panel-name[Output] ``` ## [1] 3.5 ``` ] ] --- # 😼 *That's okay but you promise to...* -- - combine plot, text, tables and images in a single file. -- - publish my work online or convert into a word, pdf or html file. -- - work efficiently with my different projects and save, share and track them. -- ## .orange[WE NEED A SUPERHERO ... ] --- class: center, middle, hide-count, hide-logo background-image: url("https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Flat.png") background-size: contain --- class: hide-logo background-image: url("https://res-5.cloudinary.com/crunchbase-production/image/upload/c_thumb,h_256,w_256,f_auto,g_faces,z_0.7,q_auto:eco/v1398402407/chvmdsmomg96uoz8g3ad.jpg") background-size: 20% background-position: 99% 1% # 😻 About RStudio: - 2009, Joseph J. Allaire founded RStudio. -- - 2011, RStudio IDE for R was launched. -- - "RStudio is dedicated to sustainable investment in free and open-source software for data science." (16 Jan 2022) .footnote[ [Source](https://www.rstudio.com/about/) ] --- class: center # R `\(\rightleftharpoons\)` RStudio -- .pull-left[ ## Imagine RStudio as a stylish car ... # .big-text[🚗] ] -- .pull-right[ ## and R as its powerful engine. # .big-text[⚙️] ] --- class: center # RStudio IDE <img src="images/rstd-ide.png" width="75%" style="display: block; margin: auto;" /> --- class: center ### RStudio `\(\rightarrow\)` Tools `\(\rightarrow\)` Global Options <img src="images/rstd-options.png" width="50%" style="display: block; margin: auto;" /> --- class: center ### RStudio `\(\rightarrow\)` Tools `\(\rightarrow\)` Global Options <img src="images/rstd-options2.png" width="50%" style="display: block; margin: auto;" /> --- background-image: url("images/learn.png") background-size: 125% background-color: #F5F4F2 .footnote[ [Source](https://education.rstudio.com) ] --- class: center middle # R Program <img src="images/data-science.png" width="100%" style="display: block; margin: auto;" /> .footnote[ [Source](https://r4ds.had.co.nz/introduction.html) ] --- class: center middle # Data Wrangling <img src="images/data-science-wrangle.png" width="100%" style="display: block; margin: auto;" /> .footnote[ [Source](https://r4ds.had.co.nz/introduction.html) ] --- class: center middle # Exploratory Data Analysis <img src="images/data-science-explore.png" width="100%" style="display: block; margin: auto;" /> .footnote[ [Source](https://r4ds.had.co.nz/introduction.html) ] --- class: center middle # Modeling <img src="images/data-science-model.png" width="100%" style="display: block; margin: auto;" /> .footnote[ [Source](https://r4ds.had.co.nz/introduction.html) ] --- class: center middle # Data Visualisation <img src="images/data-science-communicate.png" width="100%" style="display: block; margin: auto;" /> .footnote[ [Source](https://r4ds.had.co.nz/introduction.html) ] --- class: center middle # R Program <img src="images/data-science-program.png" width="100%" style="display: block; margin: auto;" /> .footnote[ [Source](https://r4ds.had.co.nz/introduction.html) ] --- class: hide-count, hide-logo background-image: url("https://pbs.twimg.com/media/FJJ3qyAXwAQSI8s?format=jpg&name=medium") background-size: 70% background-position: 70% .footnote[ [Source](https://www.business-science.io/learning-r/2018/11/04/data-science-r-cheatsheet.html) ] --- class: center, middle, hide-count, hide-logo background-image: url("images/space-pkgs.png") background-position: 110% background-size: contain .pull-left[ # .big-text[R<br>Packages] ] --- # R Packages: - On 12 Jan 2022, 18698 R packages were available at [CRAN](https://cran.r-project.org/web/packages/) -- - "An R package is a collection of functions, data, and documentation that extends the capabilities of base R. Using packages is key to the successful use of R." -- - Top downloaded packages [source](https://www.r-pkg.org/downloaded) .footnote[ [Source](https://r4ds.had.co.nz/introduction.html) ] --- class: center # To Download pkgs <img src="images/pkg-download1.png" width="65%" style="display: block; margin: auto;" /> --- class: center # Name of the R package(s) <img src="images/pkg-download2.png" width="65%" style="display: block; margin: auto;" /> --- class: center # Installed R package(s) <img src="images/pkg-download3.png" width="45%" style="display: block; margin: auto;" /> --- # R Function to Download Package ```r *install.packages("tidyverse") ``` -- # R Function to use Package ```r *library(tidyverse) ``` --- # About R Packages: - You need to install package only once like - 📚 We buy books once and use them again and again -- - 💡 Fix the bulb once and use it again and again -- - In every R document you need to `call once` the package using function `library()`, for example library(ggplot2). -- - Once in a while, you need to update the installed packages as well. -- - If you un-install R or RStudio, you will lose all installed packages. --- class: center # Tools `\(\rightarrow\)` Check Package Updates <img src="images/update-pkg1.png" width="42%" style="display: block; margin: auto;" /> --- class: center # Select Package(s) to Update <img src="images/update-pkg2.png" width="60%" style="display: block; margin: auto;" /> --- class: center # Click Install Updates <img src="images/update-pkg3.png" width="60%" style="display: block; margin: auto;" /> --- class: center # To Remove Package(s) <img src="images/remove-pkg.png" width="40%" style="display: block; margin: auto;" /> --- class: inverse, your-turn, hide-logo # 🧠 YOUR TURN .panelset[ .panel[.panel-name[Task] - Install R packages `xaringan` & `palmerpenguins` - Call R packages using console ] .panel[.panel-name[Answer] ```r install.packages(c("xaringan", "palmerpenguins")) library(xaringan) library(palmerpenguins) ``` ] ]
−
+
05
:
00
--- class: hide-count, center, middle background-image: url("images/heartyr.gif") background-size: contain background-position: 5% # .right.big-text[Objects] --- # R Object ##- "Just a name that you can use to call up stored data" .footnote[ Source: [RStudio](https://rstudio-education.github.io/hopr/basics.html) ] --- # .center[Create Object] .panelset[ .panel[.panel-name[Code] ```r *salary <- c(20, 30, 40, 50, -10) *salary ``` ] .panel[.panel-name[Output] ``` ## [1] 20 30 40 50 -10 ``` ] ] --- # .center[Create Object] .panelset[ .panel[.panel-name[Code] ```r name <- c("Ram", "Rani", "Ali", "Preeti", "John") name ``` ] .panel[.panel-name[Output] ``` ## [1] "Ram" "Rani" "Ali" "Preeti" "John" ``` ] ] --- # .center[Create Object] .panelset[ .panel[.panel-name[Code] ```r age <- c(34, 54, 23, 65, 2 ) age ``` ] .panel[.panel-name[Output] ``` ## [1] 34 54 23 65 2 ``` ] ] --- # .center[Create Object] .panelset[ .panel[.panel-name[Code] ```r place <- c("ny", "ber", "dhl", "tko", "lon") place ``` ] .panel[.panel-name[Output] ``` ## [1] "ny" "ber" "dhl" "tko" "lon" ``` ] ] --- # .center[Create Object] .panelset[ .panel[.panel-name[Code] ```r books <- c(4, 0, 3, 24, 5) books ``` ] .panel[.panel-name[Output] ``` ## [1] 4 0 3 24 5 ``` ] ] --- # Guidelines to name `objects` in R: - a name cannot start with a number -- - a name cannot use some special symbols, like ^, !, $, @, +, -, /, or *: -- - avoid caps -- - avoid space -- - use dash (like na-me) or underscore (like na_me) -- - if chronology matters then add date (2020-09-05-file-name) --- class: center # RStudio Environment Window <img src="images/env-rstd.png" width="70%" style="display: block; margin: auto;" /> -- ## 🤔.i.green[how to combine these<br> objects/variables into a data or say tidy data] --- exclude: true class: hide-count, hide-logo background-image: url("images/tidydata_7.jpg") background-size: cover --- exclude: true class: hide-count, hide-logo background-image: url("images/tidydata_1_1.jpg") background-size: contain --- exclude: true class: hide-count background-image: url("images/tidydata_1_2.jpg") background-size: contain --- exclude: true class: hide-count background-image: url("images/tidydata_2_1.jpg") background-size: contain --- exclude: true class: hide-count background-image: url("images/tidydata_2_2.jpg") background-size: contain --- class: center # Tidy data 👇 😻😻😻 ``` ## age books name place salary ## 1 34 4 Ram ny 20 ## 2 54 0 Rani ber 30 ## 3 23 3 Ali dhl 40 ## 4 65 24 Preeti tko 50 ## 5 2 5 John lon -10 ``` -- <img src="images/tidy-1.png" width="70%" style="display: block; margin: auto;" /> .footnote[ [Source](https://r4ds.had.co.nz/index.html) ] --- # .center[How to create a data object?] .panelset[ .panel[.panel-name[Code] ```r *social <- data.frame(age, books, name, place, salary) social ``` ] .panel[.panel-name[Output] ``` ## age books name place salary ## 1 34 4 Ram ny 20 ## 2 54 0 Rani ber 30 ## 3 23 3 Ali dhl 40 ## 4 65 24 Preeti tko 50 ## 5 2 5 John lon -10 ``` ] ] --- # .center[How to export data as a csv file?] .panelset[ .panel[.panel-name[Code] ```r library(readr) # to save this data set as a csv file *write_csv(social, "data/social.csv") ``` ] .panel[.panel-name[Output] ] ] --- class: your-turn, hide-count, hide-logo # 🧠 YOUR TURN ## .yellow[Write codes for below dataframe] .panelset[ .panel[.panel-name[Task] ``` ## state pop capital foundation ## 1 Germany 20 Berlin 1870-12-10 ## 2 France 19 Paris 1789-07-14 ## 3 India 50 Delhi 1947-08-15 ## 4 Russia 25 Moscow 1990-06-12 ## 5 USA 30 Washington 1776-07-04 ## 6 New Zealand 5 Wellington 1840-02-06 ``` ] .panel[.panel-name[Answer] ```r state <- c("Germany", "France", "India", "Russia", "USA", "New Zealand") pop <- c(20, 19, 50, 25, 30, 5) capital <- c("Berlin", "Paris", "Delhi", "Moscow", "Washington", "Wellington") foundation <- c("1870-12-10", "1789-07-14", "1947-08-15", "1990-06-12", "1776-07-04", "1840-02-06") world <- data.frame(state, pop, capital, foundation) world ``` ] ]
−
+
10
:
00
--- # .center[Get a List of all Objects] .panelset[ .panel[.panel-name[Code] ```r # names of created objects *objects() ``` ] .panel[.panel-name[Output] ``` ## [1] "age" "books" "capital" "foundation" "name" ## [6] "place" "pop" "salary" "soc" "social" ## [11] "state" "world" ``` ] ] --- class: center, middle, hide-count background-image: url("images/breakr.gif") background-size: 50% background-position: 90% .pull-left[ # .big-text[Need Help!] ] --- class: center # Using Console `>` ## in console type `?your query` -- <br><br>.green[for example] `?ggplot` --- class: center # RStudio: pkg Help Docs <img src="images/help-cran.png" width="60%" style="display: block; margin: auto;" /> --- class: center # [RStudio Community](https://community.rstudio.com/) <br> <img src="images/help-rstd.png" width="90%" style="display: block; margin: auto;" /> --- class: center # [stackoverflow](https://stackoverflow.com/) <br> <img src="images/help-stack.png" width="90%" style="display: block; margin: auto;" /> --- class: center # [GitHub Issues](https://github.com/) <img src="images/help-git.png" width="100%" style="display: block; margin: auto;" /> --- class: center # [Twitter](https://twitter.com/home) #rstats <img src="images/help-tweet.png" width="60%" style="display: block; margin: auto;" /> --- class: center middle hide-count # 🙋🏽♀️🙋♂️<br>.big-text[Q&A] --- class: center, middle, inverse, hide-logo # Dynamic Documents<br>using R Markdown ### .orange[Next Module - 2]