class: title-slide, center, middle # An Introduction to R ## Why Learn to Code? ### · Alex Douglas · ### University of Aberdeen <!-- #### BI5009 · 2019 --> --- class: inverse, right, bottom <img style="border-radius: 50%;" src="https://github.com/alexd106.png" width="200px"/> ### Find me at... .medium[ [<svg viewBox="0 0 496 512" xmlns="http://www.w3.org/2000/svg" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> Alex Douglas](https://github.com/alexd106) [<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;"> <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> @Scedacity](https://twitter.com/scedacity) [<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;"> <path d="M440 6.5L24 246.4c-34.4 19.9-31.1 70.8 5.7 85.9L144 379.6V464c0 46.4 59.2 65.5 86.6 28.6l43.8-59.1 111.9 46.2c5.9 2.4 12.1 3.6 18.3 3.6 8.2 0 16.3-2.1 23.6-6.2 12.8-7.2 21.6-20 23.9-34.5l59.4-387.2c6.1-40.1-36.9-68.8-71.5-48.9zM192 464v-64.6l36.6 15.1L192 464zm212.6-28.7l-153.8-63.5L391 169.5c10.7-15.5-9.5-33.5-23.7-21.2L155.8 332.6 48 288 464 48l-59.4 387.3z"></path></svg> a.douglas@abdn.ac.uk](mailto:a.douglas@abdn.ac.uk) ] --- class: center, middle # Let's get started... <img src = "images/any_key.gif" width = 600px> --- class: left, middle # learning outcomes .medium[ **this course (hopefully) will ...** - introduce you to using R and RStudio ✔️ - help your research become more robust and reproducible ✔️ - show you how to import your data ✔️ - show you how to summarise, visualise and analyse your data ✔️ **this course (definitely) won't ...** - teach you everything there is to know about R ❌ - make you feel completely comfortable with R ❌ ] --- background-image: url(images/cover_v3.png) background-size: 200px background-position: 90% 60% class: left, middle # course structure .medium[ at the heart of this course is our open access [Introduction to R](https://alexd106.github.io/Rbook/) book - introduction to R and RStudio environments - perform basic operations in R - importing exploring, wrangling and summarising data into R - visualising data using base R and ggplot graphics - introduce some simple statistics using R - also other Chapters on programming in R, using R markdown and version control with Git and GitHub ] --- background-image: url(images/intro2R.png) background-size: 450px background-position: 95% 55% class: left, middle # course structure .medium[ and also our [intro2R](https://alexd106.github.io/intro2R/) website - series of exercises (with solutions) to work through - exercises cover material from each Chapter - short how-to videos covering key R skills - tutorials covering additional topics - many links to other resources ] --- background-image: url(images/intro2R.png) background-size: 450px background-position: 95% 55% class: left, middle # suggested approach .medium[ - work through material for each Chapter - once you've finished a Chapter, test your understanding by completing the associated exercise - don't peek at the solutions too quickly 😄 - create an R script to document your code (preferably in an RStudio Project) - use R's help pages and Google when you get stuck! ] --- background-image: url(images/Rlogo.png) background-size: 200px background-position: 95% 95% class: left, top # What is R? <br> .medium[ - answer depends on who you ask and what they use R for - environment for statistical computing, graphics and programming - originally created by Ross Ihaka and Robert Gentleman (1996) - currently maintained by international R-core development team - many, many people also contribute to R and the wider R community ] --- background-image: url(images/r-project.png) background-size: 500px background-position: 95% 50% class: middle, left .medium[ - more information can be found at https://www.r-project.com - download R from the CRAN website https://cran.uk.r-project.org - see [here](https://alexd106.github.io/intro2R/howto.html) for a short video describing how to download and install R ] --- class: left, middle .center[ # why is R considered hard? ] .pull-left[ .medium[ - high initial investment in time to learn R - unfamiliar command line environment - frustrating (and sometimes) inconsistent syntax - analysis and figures can take longer (initially!) - relatively steep learning curve - it's not actually that hard, just unfamiliar ] ] .pull-right[ <img src = "images/bin.gif" width = 900px height = 275px> ] --- background-image: url(https://i0.wp.com/r4stats.com/wp-content/uploads/2017/06/Fig_2d_ScholarlyImpact2016.png) background-size: 450px background-position: 95% 70% class: middle, left # why you should learn R .medium[ - it’s free and platform independent - it’s the software of choice for many students, academics, industries and charities worldwide - highly flexible and extensive - encourages you think about your research questions, data and analyses ] .footnote[ [Muenchen (2019)](http://r4stats.com/articles/popularity/) ] --- background-image: url(images/repo2.png) background-size: 1000px background-position: 90% 90% class: top, left <br> # why you should learn R .medium[ - it allows you to keep an exact and reproducible record of your analyses - transparent - other people* can reproduce your analysis - easily share your code (GitHub) - [open science](https://en.wikipedia.org/wiki/Open_science) ] .footnote[ <sup>* including the future you! ] --- background-image: url(images/plots.png) background-size: 850px background-position: 50% 95% class: top, left # why you should learn R .medium[ - excellent graphics capabilities ] .footnote[ [ggplot2](https://github.com/tidyverse/ggplot2) ] --- background-image: url(images/jobs.png) background-size: 1000px background-position: 95% 80% class: top, left # why you should learn R .medium[ - employability - opportunity to get involved with a fantastic and supportive community ] --- background-image: url(images/rconsole.png) background-size: 850px background-position: 50% 70% .center[ # using R - GUI ] --- class: center, middle .pull-left[ .large[ R ] <img src = "images/engine.png" width = 425px> ] .pull-right[ .large[ RStudio ] <img src = "images/car.png" width = 425px> ] --- background-image: url(images/Rstudio_2.png) background-size: 1000px background-position: 50% 95% class: top, top .center[ # using R - RStudio ] --- class: left, top <br> # using R - objects .medium[ - commands are typed in the editor and then sourced into the console at the > prompt ```r > 2 + 2 ## [1] 4 ``` - R is object orientated. You can create variable and assign value(s) to them ```r > a <- 2 + 2 > a ## [1] 4 ``` ] --- class: left, top <br> # using R - objects .medium[ - once created, operations can be performed on variables ```r > a <- 2 + 2 > b <- 3 * 2 > > a + b ## [1] 10 ``` - this is very powerful and flexible - much of the functionality of R is enhanced by using variables called functions ] --- class: left, top <br> # using R - functions .medium[ - functions contain a set of instructions that allow you to perform a specific task(s) - you can use functions that are inbuilt in R ```r > numbers <- c(2, 3, 4, 5, 6) # use the concatenate function > numbers ## [1] 2 3 4 5 6 > > mean(numbers) # use mean function ## [1] 4 > > var(numbers) # use variance function ## [1] 2.5 ``` - comments (#) are ignored by R ] --- background-image: url(images/packages.png) background-size: 800px background-position: 50% 52% class: left, top # using R - functions .medium[ - or you can install user contributed packages to increase versatility and power - there are packages for almost anything <br><br><br><br> - install packages from [CRAN](https://cran.r-project.org/), [Bioconductor](https://www.bioconductor.org/) and [GitHub](https://github.com/) - packages are easy to install in R ] .footnote[ [RDocumentation (2019)](https://www.rdocumentation.org/trends) ] --- class: left, top <br> # using R - functions .medium[ - or write your own functions - function to calculate standard error <br> ```r > numbers <- c(2, 3, 4, 5, 6) # use the concatenate function > > se.fnc <- function(x){ # start function for se + std.x <- sd(x) # calculate SD + nos.x <- length(x) # calculate number obs + se.x <- std.x/(sqrt(nos.x)) # calculate SE + print(se.x) + } > > se.fnc(numbers) # use your new function ## [1] 0.7071068 ``` ] --- class: left, top <br> # using R - syntax .medium[ - R is case sensitive `A` is not the same as `a` - commands are generally separated by a new line, but you can also use a `;` (rare) - anything that follows the hash symbol (#) will be ignored by R. Use this to comment your code - a series of commands can be grouped using braces `{ }` - write pretty code by following a [code style guide](https://google.github.io/styleguide/Rguide.html) ] --- background-image: url(images/Rstudio_3.png) background-size: 500px background-position: 50% 85% class: left, top # using R - plots .medium[ - by default plots are displayed in the 'Plots' pane in RStudio - plots can be copied and pasted into your word processor of choice - better to export plots to an external file (jpeg, pdf, png etc) ] --- class: left, top <br> # using R - help .medium[ - R has extensive help facilities - from within R the main method of getting help is to use the `help()` function ```r > help("plot") # open help file for the function plot ``` or equivalently ```r > ?plot ``` - or to search the help files use `help.search("plot")` or `??plot` ] --- class: left, top <br> # using R - help .medium[ - [Google it!](http://lmgtfy.com/?q=R+Error+in+library+there+is+no+package+called): Try Googling any error messages you get. It's not cheating and everyone does it! You'll be surprised how many other people have probably had the same problem and solved it. - [Stack Overflow](http://stackoverflow.com/questions/tagged/r): There are many thousands of questions relevant to R on Stack Overflow - [Here](https://stackoverflow.com/questions/tagged/r?sort=votes) are the most popular ones, ranked by vote. Make sure you search for similar questions before asking your own - make sure you include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) to get the most useful advice. A reproducible example is a minimal example that lets others who are trying to help you to see the error themselves. ] --- class: left, top # tips for learning R .medium[ - use R often and use it regularly - learning R is not a memory test, you have your R scripts - you don’t need to know everything there is to know about R to use it productively - staring at code for hours is not good for you. Walk away for a few minutes - there are many ways to tackle a particular problem. See what others have done - recognise that there will be times when things will get a little tough or frustrating. Try to accept these periods as part of the natural process of learning a new skill (we’ve all been there) and remember, the time and energy you invest now will be more than payed back in the not too distant future. ] --- class: center, middle # Let's get started... <img src = "images/force.gif" width = 600px> ---