+ - 0:00:00
Notes for current slide
Notes for next slide

An Introduction to R

Why Learn to Code?

· Alex Douglas ·

University of Aberdeen

1 / 29

Let's get started...

3 / 29

learning outcomes

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 ❌

4 / 29

course structure

at the heart of this course is our open access
Introduction to R 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

5 / 29

course structure

and also our 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

6 / 29

suggested approach

  • 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!

7 / 29

What is R?


  • 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

8 / 29
9 / 29

why is R considered hard?

  • 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

10 / 29

why you should learn R

  • 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

11 / 29


why you should learn R

  • 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

* including the future you!

12 / 29

why you should learn R

  • excellent graphics capabilities
13 / 29

why you should learn R

  • employability

  • opportunity to get involved
    with a fantastic and supportive
    community

14 / 29

using R - GUI

15 / 29

R

RStudio

16 / 29

using R - RStudio

17 / 29


using R - objects

  • commands are typed in the editor and then sourced into the console
    at the > prompt
> 2 + 2
## [1] 4
  • R is object orientated. You can create variable and assign value(s) to them
> a <- 2 + 2
> a
## [1] 4
18 / 29


using R - objects

  • once created, operations can be performed on variables
> 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

19 / 29


using R - functions

  • functions contain a set of instructions that allow you to perform a specific task(s)

  • you can use functions that are inbuilt in 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
20 / 29

using R - functions

  • or you can install user contributed packages to increase versatility and power

  • there are packages for almost anything





21 / 29


using R - functions

  • or write your own functions

  • function to calculate standard error


> 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
22 / 29


using R - syntax

  • 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

23 / 29

using R - plots

  • 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)

24 / 29


using R - help

  • R has extensive help facilities

  • from within R the main method of getting help is to use the help() function

> help("plot") # open help file for the function plot

or equivalently

> ?plot
  • or to search the help files use help.search("plot") or ??plot
25 / 29


using R - help

  • Google it!: 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: There are many thousands of questions relevant to R on Stack Overflow

  • Here 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 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.

26 / 29

tips for learning R

  • 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.

27 / 29

Let's get started...

28 / 29
29 / 29
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow