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 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
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
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
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 of R Familiarisation with the R studio environment Packages for the extension of R functionality The R language
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
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
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
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
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
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
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
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 open source The R language is based on the S language developed by John Chambers in 1976 while at Bell Laboratories specifically for statistical computing
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 on 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
An introduction to R Introduction The power and limitations of R The power of R Designed for vector and matrix arithmatic enabling faster calaculation over larger matrices than possible with object orientated languages such as C++, Java and Python Designed as a script based language rather than object orientated although does allow meta-programming for object like tasks Supports parallel processing and multithreading Can implement other languages e.g. C, C++, Fortran, Python and LaTex
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
An introduction to R Introduction Familiarisation with the R studio environment R Scripting window Script window - write and run an R script
An introduction to R Introduction Familiarisation with the R studio environment The variable explorer Variable explorer - lists active variables/objects
An introduction to R Introduction Familiarisation with the R studio environment The plot window Plot window - displays and retains all plots
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 open 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
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’
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
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)
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
Recommend
More recommend