introduction to r (and programming) to programmin… · introduction to r (and programming) what is...
TRANSCRIPT
IntroductiontoR(andprogramming)
WhatisR?
• Programminglanguage• Toolforstatistics
• WHYR?
RStudio• IntegratedDevelopmentEnvironment
Programming
• Wayofthinking• Mathmatics• Logicbased
• Usesdefinedworkflow/instructionsandreturnsresults
Functions• GroupsofStepsthatcompleteaprocess• Eachfunctiontakesinput(parameters)andgivesaresult(output)
FunctionToBuildTable• FunctionsaretheInstructionmanuals• Needcorrecttools/materials
FunctionToBuildTable• FunctionsaretheInstructionmanuals• Needcorrecttools/materials
FunctionToBuildTable• FunctionsaretheInstructionmanuals• Needcorrecttools/materials
FunctionToBuildTable
FunctionToBuildTable
FunctionToBuildTable
FunctionToBuildTable
FunctionToBuildTable
ThinkingLikeaCode
AveragingNumbers
ThinkingLikeaCode
Average<- function(num1,num2,num3,n){num1+num2+num3/n}
DataStorage
• Storeinformationinobjects• Objectshavenames
LEGSNAILS
TableTOP
ObjectTypes
LEGSNAILS
TableTOP
WOOD FASTENERS WOOD
ObjectTypes
LEGS NAILS
WOOD FASTENERS
ObjectTypes
LEGSNAILS
TableTOP
WOOD FASTENERS WOOD
ObjectTypes
TableTOP
NAILS
LEGS
UseCorrectObjectTypes
UseCorrectObjectTypes
TypesofObjects
• Vector• Dataframe• List• matrix
TypesofObjects
• Vector• Dataframe• List• matrix
TypesofVariables
• numeric• string• date
Vector
• 1rowofdata• Allthesametypeofdata
Vector
• 1rowofdata• Allthesametypeofdata
Vector
• 1rowofdata• Allthesametypeofdata
Dataframe
• Rowsandcolumns• ‘spreadsheet’
List– BoxofBoxes
LEGS NAILS TableTOP
WOOD FASTENERS WOOD
LEGS NAILS TableTOP
WOOD FASTENERS WOOD
OtherObjectTypes
• Matrix• Array• Date/Time• Spatial• Formula• Model• …etc…
ThinkinginCode
CreateaVectorof7numbers
ThinkinginCode
1 2 3 4 5 6 7
15 32 33 17 1 5 11
myNUMs <- c(15,32,33,17,1,5,11)
ThinkingLikeaCode
AveragingNumberswhentheParameterisavector
Canonlydo1thingatatime,createaverage
1 2 3 4 5 6 7
15 18 7 17 9 3 15
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
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]
……
Average<- function(nums){sum=nums[1]for(onenum innums ){
sum=sum+onenum}sum/length(nums)}
Average<- function(nums){sum=nums[1]for(onenum innums ){
sum=sum+onenum}sum/length(nums)}
Average(mynums)
Builtinfunctions
• nums <- select(x=1:50,size=7,replace=TRUE)
• Mean(nums)
LibrariesFunctions
UsingLibraries
• Installlibrary• install.packages(“tidyverse")
• Loadlibrary• library(tidyverse)
Otherhelp
• Stackoverflow- https://stackoverflow.com/• Rbloggers- https://www.r-bloggers.com/• Quick-R- http://www.statmethods.net/• GOOGLE
BeginnerCourses
• Udemy:https://www.udemy.com/r-basics
• DataCamp:https://www.datacamp.com/courses/free-introduction-to-r
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
Checkingdata
• Whattypeofobjectismydata?
• Whatdoesitlooklike?
• Isitstoredcorrectly?
Challenges
• Howmanyandwhattypeofplottypesintheexperiment?• Howmanyspeciescaught?• Howmanyspeciesofbirds?Rodents?• AverageweightofMaleRodents?• AverageweightofFemaleRodents?