An introduction to R S. Manzi & A. Salmon South West Peninsula - - PowerPoint PPT Presentation

an introduction to r
SMART_READER_LITE
LIVE PREVIEW

An introduction to R S. Manzi & A. Salmon South West Peninsula - - PowerPoint PPT Presentation

An introduction to R An introduction to R S. Manzi & A. Salmon South West Peninsula Collaboration for Leadership in Applied Health Research and Care (PenCLAHRC) University of Exeter January 2019 An introduction to R Outline 1 Aims and


slide-1
SLIDE 1

An introduction to R

An introduction to R

  • S. Manzi & A. Salmon

South West Peninsula Collaboration for Leadership in Applied Health Research and Care (PenCLAHRC) University of Exeter

January 2019

slide-2
SLIDE 2

An introduction to R

Outline

1 Aims and topics of the training

Aims of the training Training topics – Introduction Training topics – Core R functionality Training topics – Data visualisation and statistics

2 Introduction

Installing R and R studio

Windows installation Linux installation

The power and limitations of R Familiarisation with the R studio environment Setting the working directory Packages for the extension of R functionality R conventions

slide-3
SLIDE 3

An introduction to R Aims and topics of the training

Outline

1 Aims and topics of the training

Aims of the training Training topics – Introduction Training topics – Core R functionality Training topics – Data visualisation and statistics

2 Introduction

Installing R and R studio

Windows installation Linux installation

The power and limitations of R Familiarisation with the R studio environment Setting the working directory Packages for the extension of R functionality R conventions

slide-4
SLIDE 4

An introduction to R Aims and topics of the training Aims of the training

Aims of this training

To familarise you with purpose and use of R and R studio To provide you with an understanding of the basic data structures and functionality of R To provide you with the skills to produce data visualisations in R To introduce the use of packages for extending the functionality of R

slide-5
SLIDE 5

An introduction to R Aims and topics of the training Training topics – Introduction

Training topics – Introduction

Introduction

Installation of R and R studio The power and limitations

  • f R

Familiarisation with the R studio environment Packages for the extension of R functionality The R language

slide-6
SLIDE 6

An introduction to R Aims and topics of the training Training topics – Core R functionality

Training topics – Core R functionality

Core R functionality and skills

Sequence generation and replication Variable types and conversion Data structures and conversion Vector arithmatic Identifying unique values Array sizes String concatenation and splitting Conditional logic and functions Reading from and writing to files Subscripting and subsetting Merging and appending data Sorting data Date and time data handling Tablulation Loops and the apply based functions User defined functions

slide-7
SLIDE 7

An introduction to R Aims and topics of the training Training topics – Data visualisation and statistics

Data visualisation and statistics

Plotting

Scatter and line graphs Histograms Bar charts Statistical process control charts

Statistics

Descriptive statistics T-tests Linear regression

slide-8
SLIDE 8

An introduction to R Introduction

Outline

1 Aims and topics of the training

Aims of the training Training topics – Introduction Training topics – Core R functionality Training topics – Data visualisation and statistics

2 Introduction

Installing R and R studio

Windows installation Linux installation

The power and limitations of R Familiarisation with the R studio environment Setting the working directory Packages for the extension of R functionality R conventions

slide-9
SLIDE 9

An introduction to R Introduction Installing R and R studio

Windows installation – base R

Go to the R project website https: //www.r-project.org/ Click on ’download R’ in the get started section R uses a hosting structure called CRAN (Comprehensive R Archive Network). Scroll down the list and select your local CRAN mirror. This will most likely be Bristol

slide-10
SLIDE 10

An introduction to R Introduction Installing R and R studio

Windows installation – base R

Select ’Download R for Windows’ Click on the ’base’ link as we want to install base R. Click on ’Download R 3.5.2 for Windows Your download should begin Launch the .exe installer and follow the on screen instructions

slide-11
SLIDE 11

An introduction to R Introduction Installing R and R studio

Windows installation – R Studio

Go to the R Studio download page

https://www.rstudio.com/ products/rstudio/ download/

Select the Windows installer Your download should begin Launch the .exe installer and follow the on screen instructions

slide-12
SLIDE 12

An introduction to R Introduction Installing R and R studio

Linux installation - R and R Studio

The R base is available through the Ubuntu software center or you can install it from the command line using sudo apt install rbase RStudio can be installed in a couple of different ways but the easiest (thanks Mike) seems to be:

Download the R studio installer from https: //download1.rstudio.org/rstudio-1.1.463-amd64.deb Go to your downloads folder and open a terminal there then type sudo dpkg -i rstudio...(use tab complete to get the full file name) To complete the installation some dependencies need to be

  • installed. Use sudo apt install -f

R Studio should now be installed

slide-13
SLIDE 13

An introduction to R Introduction The power and limitations of R

S as the basis for R

Developed as part of the GNU project – free and

  • pen source

The R language is based on the S language developed by John Chambers in 1976 while at Bell Laboratories specifically for statistical computing

slide-14
SLIDE 14

An introduction to R Introduction The power and limitations of R

The development of R

R was created by Ross Ihaka and Robert Gentleman at the University of Auckland R was named partly after the first names of the two creators and partly as a play

  • n the name of S.

The project began in 1992, the first version was released in 1995 and a stable beta version was released in 2000

slide-15
SLIDE 15

An introduction to R Introduction The power and limitations of R

The power of R

Designed for vector and matrix arithmatic enabling faster calaculation

  • ver larger matrices than possible with
  • bject orientated languages such as

C++, Java and Python Designed as a script based language rather than object orientated although does allow meta-programming for

  • bject like tasks

Supports parallel processing and multithreading Can implement other languages e.g. C, C++, Fortran, Python and LaTex

slide-16
SLIDE 16

An introduction to R Introduction Familiarisation with the R studio environment

The R console

R console window - directly input commands into R, view inputs and outputs

slide-17
SLIDE 17

An introduction to R Introduction Familiarisation with the R studio environment

R Scripting window

Script window

  • write and run

an R script

slide-18
SLIDE 18

An introduction to R Introduction Familiarisation with the R studio environment

The variable explorer

Variable explorer - lists active variables/objects

slide-19
SLIDE 19

An introduction to R Introduction Familiarisation with the R studio environment

The plot window

Plot window - displays and retains all plots

slide-20
SLIDE 20

An introduction to R Introduction Setting the working directory

Setting your working directory in R Studio

Navigate to the tools drop down on the top tool bar in R Studio Select global options which

  • pen a new window

The first option you will see is the working directory setting Browse or manually enter the location of your R working directory

slide-21
SLIDE 21

An introduction to R Introduction Setting the working directory

Setting your working directory in an R script

Normally you will want to place your R scripts in specific sub-folders of your working directory to keep things organised The command to set the working directory manually in a script is setwd(’folder location’) When reading or writing anything to and from sub-folders of your working directory you need to add the sub-folder name(s) in the standard drive location format e.g. ’/subfolder/subsubfolder/subsubsubfolder’

slide-22
SLIDE 22

An introduction to R Introduction Packages for the extension of R functionality

Installing and using packages

R makes use of packages to extend its base functionality Packages need to be installed for each distribution of R that you run Packages are installed in R studio by going to the tools drop down on the top tool bar, selecting install packages, searching for the required package and selecting install

slide-23
SLIDE 23

An introduction to R Introduction Packages for the extension of R functionality

Installing and using packages

Packages can also be installed from the R command line using the command install.packages() function Packages need to be initialised in an R script. This is done by convention at the start of the script using library(package name)

slide-24
SLIDE 24

An introduction to R Introduction R conventions

R does not require the use of correct indentations for the code to run however, convention dictates that standard indents are used The # (hash) symbol is used to insert comments into the code R uses specific assignment operators

< − is assign the output from the right to the left = is the same as ¡- but it is poor practice to use this operator in this way < − is assign the output from the left to the right << − is assign the out from the right to the global/parent variable on the left − >> is assign the out from the left to the global/parent variable on the right