introduction to r (and programming) to programmin… · introduction to r (and programming) what is...

46
Introduction to R (and programming)

Upload: others

Post on 02-Jun-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

IntroductiontoR(andprogramming)

Page 2: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

WhatisR?

• Programminglanguage• Toolforstatistics

• WHYR?

Page 3: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

RStudio• IntegratedDevelopmentEnvironment

Page 4: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Programming

• Wayofthinking• Mathmatics• Logicbased

• Usesdefinedworkflow/instructionsandreturnsresults

Page 5: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Functions• GroupsofStepsthatcompleteaprocess• Eachfunctiontakesinput(parameters)andgivesaresult(output)

Page 6: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable• FunctionsaretheInstructionmanuals• Needcorrecttools/materials

Page 7: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable• FunctionsaretheInstructionmanuals• Needcorrecttools/materials

Page 8: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable• FunctionsaretheInstructionmanuals• Needcorrecttools/materials

Page 9: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable

Page 10: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable

Page 11: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable

Page 12: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable

Page 13: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

FunctionToBuildTable

Page 14: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ThinkingLikeaCode

AveragingNumbers

Page 15: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ThinkingLikeaCode

Average<- function(num1,num2,num3,n){num1+num2+num3/n}

Page 16: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

DataStorage

• Storeinformationinobjects• Objectshavenames

LEGSNAILS

TableTOP

Page 17: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ObjectTypes

LEGSNAILS

TableTOP

WOOD FASTENERS WOOD

Page 18: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ObjectTypes

LEGS NAILS

WOOD FASTENERS

Page 19: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ObjectTypes

LEGSNAILS

TableTOP

WOOD FASTENERS WOOD

Page 20: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ObjectTypes

TableTOP

NAILS

LEGS

Page 21: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

UseCorrectObjectTypes

Page 22: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

UseCorrectObjectTypes

Page 23: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

TypesofObjects

• Vector• Dataframe• List• matrix

Page 24: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

TypesofObjects

• Vector• Dataframe• List• matrix

TypesofVariables

• numeric• string• date

Page 25: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Vector

• 1rowofdata• Allthesametypeofdata

Page 26: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Vector

• 1rowofdata• Allthesametypeofdata

Page 27: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Vector

• 1rowofdata• Allthesametypeofdata

Page 28: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Dataframe

• Rowsandcolumns• ‘spreadsheet’

Page 29: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

List– BoxofBoxes

LEGS NAILS TableTOP

WOOD FASTENERS WOOD

LEGS NAILS TableTOP

WOOD FASTENERS WOOD

Page 30: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

OtherObjectTypes

• Matrix• Array• Date/Time• Spatial• Formula• Model• …etc…

Page 31: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ThinkinginCode

CreateaVectorof7numbers

Page 32: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ThinkinginCode

1 2 3 4 5 6 7

15 32 33 17 1 5 11

myNUMs <- c(15,32,33,17,1,5,11)

Page 33: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ThinkingLikeaCode

AveragingNumberswhentheParameterisavector

Page 34: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Canonlydo1thingatatime,createaverage

1 2 3 4 5 6 7

15 18 7 17 9 3 15

Page 35: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Canonlydo1thingatatime,createaverage

1 2 3 4 5 6 7

15 18 7 17 9 3 15

15+18=33

33+7=40

40+17=57

57+9=66

66+3=69

69+15=84

84/7=12

Page 36: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Canonlydo1thingatatime,createaverage

1 2 3 4 5 6 7

15 18 7 17 9 3 15

15+18=33

33+7=40

40+17=57

57+9=66

66+3=69

69+15=84

84/7=12

sum1=nums[1]+nums[2]

sum2=sum1+nums[3]

……

Page 37: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Average<- function(nums){sum=nums[1]for(onenum innums ){

sum=sum+onenum}sum/length(nums)}

Page 38: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Average<- function(nums){sum=nums[1]for(onenum innums ){

sum=sum+onenum}sum/length(nums)}

Average(mynums)

Page 39: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Builtinfunctions

• nums <- select(x=1:50,size=7,replace=TRUE)

• Mean(nums)

Page 40: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

LibrariesFunctions

Page 41: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

UsingLibraries

• Installlibrary• install.packages(“tidyverse")

• Loadlibrary• library(tidyverse)

Page 42: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Otherhelp

• Stackoverflow- https://stackoverflow.com/• Rbloggers- https://www.r-bloggers.com/• Quick-R- http://www.statmethods.net/• GOOGLE

Page 43: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

BeginnerCourses

• Udemy:https://www.udemy.com/r-basics

• DataCamp:https://www.datacamp.com/courses/free-introduction-to-r

Page 44: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

ImportingDataColumn Description

record_id Uniqueidfortheobservation

month monthofobservationday dayofobservationyear yearofobservationplot_id IDofaparticularplotspecies_id 2-lettercodesex sexofanimal(“M”,“F”)

hindfoot_length lengthofthehindfootinmm

weight weightoftheanimalingrams

genus genusofanimalspecies speciesofanimal

taxa e.g. Rodent,Reptile,Bird,Rabbit

plot_type typeofplot

http://www.datacarpentry.org/R-ecology-lesson/02-starting-with-data.html

Page 45: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Checkingdata

• Whattypeofobjectismydata?

• Whatdoesitlooklike?

• Isitstoredcorrectly?

Page 46: Introduction to R (and programming) to programmin… · Introduction to R (and programming) What is R? •Programming language •Tool for ... Programming •Way of thinking •Mathmatics

Challenges

• Howmanyandwhattypeofplottypesintheexperiment?• Howmanyspeciescaught?• Howmanyspeciesofbirds?Rodents?• AverageweightofMaleRodents?• AverageweightofFemaleRodents?