Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to R Fish 552: Lecture 1.

Similar presentations


Presentation on theme: "Introduction to R Fish 552: Lecture 1."— Presentation transcript:

1 Introduction to R Fish 552: Lecture 1

2 Introduction Course website Syllabus
Syllabus

3 Recommended reading An Introduction to R (R Development Core Team)
Chapter 1 Chapter 2: , 2.8 Chapter 3 YaRI - Yet another R Introduction (Andreas Handel) Section 3.1, 3.2 Section 4.1, 4.3 Section 5.1, 5.2, 5.5

4 What is R? R is a language and environment for statistical computing, graphics and much more It is a (open source) GNU project which is similar to the S language and environment developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers.) R can be considered as a different implementation of S with more flexibility and power gained from contributions by other users 4

5 What is R? an effective data handling and storage facility
a large, coherent, integrated collection of tools for data analysis graphical facilities for data analysis and display either on-screen or on hardcopy a well-developed, simple and effective programming language including traditional statements such as conditionals, loops, user-defined functions, and input and output facilities (Covered in FISH 553). 5

6 Get the right tool for the job
There might be better options than Excel

7 Where to get R The R-project web site:
The program can be downloaded from any one of the official mirrors of CRAN Download the compiled binary code for your operating system 7

8 What can R do? R provides a comprehensive set of statistical analysis techniques Classical statistical tests Linear and nonlinear modeling Time-series analysis Classification & cluster analysis Spatial statistics Bayesian statistics any statistical technique you use is likely built into R or a user contributed package 8

9 What can R do? Contributed Packages are a salient feature of R
Source:

10 What can R do? Publication-quality plots can be produced
Many default graphing choices The user retains full control of the graphics Even rudimentary plots like histograms can be made vibrant and exciting 10

11 Learning R Just like with other computing languages, the initial learning curve can be steep, but there are myriads of help files, online sources, books and teachers. The benefits of becoming fluent can be very rewarding Be patient and creative! 11

12 R Reference Material No required/recommended textbook in this class
Intro to R (PDF available from help menu) Many books to reference Data Analysis and Graphics Using R, 2nd ed. (Maindonald & Braun) The R Book (Crawley) A Primer of Ecology with R (Stevens) R Ref card

13

14 Online Reference Material
R Website R Seek (specific R search engine) R Wiki The Ecological Detective (Hilborn and Mangel)

15 Help within R? Searching help: help.search(“logarithm”)
Finding functions: > apropos("log") [7] "SSlogis" "as.data.frame.logical" "as.logical" "as.logical.factor" "dlogis" "is.logical" [13] "log" "log10" "log1p" "log2" "logLik" "logb" [19] "logical" "loglin" "plogis" "print.logLik" "qlogis" "rlogis" Getting help for a function help(log) ?log This font means this is an R command

16 ?log The help file is broken down into several components. This information can be dense, but is very useful. Discover other functions

17 R workspaces All analyses can be saved in an R workspace at the end of a session I don’t recommend this The location of the workspace should be specified at the beginning of an R session

18 Scripts Reproducible work (Scientific method)
Handy for rerunning similar analysis later Save your scripts ! e.g. Lab1.R All saved scripts should have *.R extension

19 RStudio Scripts (files with R code) Objects you’ve created
Plots & help R console (output)

20 Rstudio tips Press Tab to get code completion
Sends entire file to console Ctrl-Enter (⌘-Enter on mac) sends current line or selection to console

21 Some simple R Commands Use comments to document the intention of the code The # sign denotes a comment. All subsequent entries are not interpreted by R # This is a comment Rule of thumb Document the why not the what (i.e. intent, behavior, and purpose of code) Use good names instead of just relying on comments Do not use comments that repeat the code Don’t assume you will remember anything about the code when you look at it later

22 Some Simple R Commands > 2+2 [1] 4 > 2^2 > 2*(1+1)
> 2+2 [1] 4 > 2^2 > 2*(1+1) Result Grouping and ordering > 2 * 1 + 1 [1] 3 22

23 Some simple R commands > exp(0) [1] 1 > log(2.718282)
> log( , base = 10) [1] > log( +, base = 10) Optional argument What’s the default ? Incomplete command

24 Pair Programming Driver: performs the “on-computer” tasks needed to complete the program, including controlling the mouse and keyboard to enter the code Navigator: actively watches the driver’s activities, makes suggestions, points out errors and problems, asks questions, and thinks about slightly longer-term strategies Driver typically responds to the navigator’s input by taking actions at the computer or engaging the navigator in further conversation that moves the project forward Switch roles for each lab exercise

25 In-class Exercise 1 Use R to compute the following
Create a new script to save your work in 1 + 2(3 + 4) log( ) Remember to use driver/navigator roles

26 Objects Every programming outcome can be stored as a object
Numbers Characters (aka strings or text) Tables Vectors/Matrices Plots Statistical output Good names for objects are very important! Objects in R are global 26

27 Assigning Values answer <- log( ) answer = log( ) answer = log( , base = 10) Assign the value of log( ) to a new object named answer = can be used instead of <- Optional argument

28 Assigning values Characters may also be assigned to objects
myName <- "Chloe" myName <- 'Chloe' > ( myName <- "Chloe Bracis" ) [1] "Chloe Bracis" Single or double quotes may be used Spaces can enter in character variables Note that putting a command in ( ) will display the assigned object in the R Console

29 Viewing objects There are several ways to display the value of an object that has been assigned. > print(answer) [1] > answer > answer* 10 [1] manipulate an object

30 Removing objects rm(list = ls())
To list the objects in the current workspace > ls() [1] "answer" "myName" To remove an object > rm(answer) [1] "myName“ To remove all objects rm(list = ls()) Or look in the Workspace section of RStudio Be very careful when doing this – useful when starting a new analysis in the same R session Or use Workspace/Clear All… menu item in RStudio

31 Data types Data types describe how objects are stored in a computer’s memory When storing an object in R, you do not need to specify the data type Common data types (a.k.a. mode) include Numeric (Integers, Floating point numbers (double)) Logical (Booleans: True / False) Characters (Text data) An objects type is not always obvious (particularly when reading in data from external sources) and knowing exactly what it is can be very useful answer <- log( )

32 Data types > answer [1] 1 > mode(answer) [1] "numeric"
> is.numeric(answer) [1] TRUE > typeof(answer) [1] "double" answer <- as.integer(answer) is. functions useful for determining modes of objects returned by R functions Memory is cheap! R will always store a numeric object as a double unless specified. as. functions coerce the objects data type or mode as. functions convert to a different data type

33 Data types Similar functions can be applied to character variables
> is.character(answer) [1] FALSE > is.character(myName) [1] TRUE > typeof(myName) [1] "character“ Character and numeric storage modes will be the most common encountered in this class

34 Vectors A vector is an ordered collection of numbers > lengths
[1]

35 Creating vectors > c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
The following are equivalent. They all create a vector of the numbers from 1 to 10. > c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) [1] 1:10 seq(from = 1, to = 10, by = 1) seq(1, 10, 1) concatenate: combine elements to form a vector use seq function for more flexibility by default R expects arguments in this order

36 Creating vectors: rep > x <- 1:3 > x [1] 1 2 3
> rep(x, length = 10) [1] > rep(x, times = 2) [1] > rep(x, each = 2) [1] Store the resulting vector as object x rep replicates the values in x by a certain argument – default is times

37 Operations on vectors Operations on vectors work element wise
> (x <- 1:3) [1] 1 2 3 > log(x) [1] > x + 1 [1] 2 3 4 > x * 2 [1] 2 4 6

38 Operations on vectors > y <- 4:6 > x + y [1] 5 7 9 > x - y
[1] > x / y [1] > x * y [1]

39 In-class Exercise 2 Create vectors using seq() and rep() and only c() if necessary Positive integers from 1 to 99 Odd integers between 1 and 99 The numbers 1,1,1,2,2,2,3,3,3 The numbers 1,2,3,4,5,4,3,2,1,0 The fractions 1, 1/2,1/3, ….., 1/10 The numbers 1,8,27,64,125,216 Remember to switch roles from Exercise 1 (driver/navigator)

40 Using functions on vectors
Famous Mauna Loa CO2 data co2 <- c(316,316.91, , , …

41 Useful arithmetic functions
> min(co2) [1] 316 > max(co2) [1] > mean(co2) [1] > median(co2) [1] > var(co2) [1] > sd(co2) [1] > range(co2) [1] > quantile(co2) 0% % % % %

42 The length function length returns the number of elements in a vector
Very useful for flexible programming > length(co2) [1] 46 > nyears <- length(co2) > years <- seq(from=1959, length=nyears) [1]


Download ppt "Introduction to R Fish 552: Lecture 1."

Similar presentations


Ads by Google