wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible.” “The term “environment” is intended to characterize it as a fully planned and coherent system, rather than an incremental accretion of very specific and inflexible tools, as is frequently the case with other data analysis software.” From r-project.org: https://www.r-project.org/foundation/ https://www.r-project.org/foundation/Rfoundation- statutes.pdf
- Community-supported Its popularity means there is a large community of users, and a huge base of helpful resources online You have to actively learn R • Books (see syllabus) • https://www.r-bloggers.com/ • http://stackoverflow.com/questions/tagged/r • http://www.introductoryr.co.uk/R_Resources_for_Beginners.html • https://learnr.wordpress.com/ • http://blog.revolutionanalytics.com/2013/05/top-3-r-resources-for- beginners.html
program each time? • In university, you have access to paid software. May not be the case after you graduate • Invest the time into learning R, and it enables you to grow into more skills. Invest the time into a specific program, and all you learn is that program • Packages often get improved over time by the community – and you always get access to the improved version (contrast w/ SPSS)
be made in Excel, SPSS, etc. you will hit a wall Most fisheries research occurs beyond this point With R, the wall is your skillfulness, not the software environment
- Community-supported - Expandable - Based on scripting This is probably the most important aspect of R Scripts are sets of instructions given to the computer that make it do a task. Scripts are recorded in plain text files with the file type of .R
Basic: It makes your code easier to read Advanced: It adds some advanced features (projects, Rpubs and Markdown, and a few other things) https://www.rstudio.com/ RStudio extends the functionality of R, and makes it easier to use
in Excel, saving CSV files) • You rarely enter data into the console. You mostly spend your time writing and running scripts Brett, this is dumb. My stats are going to be simple – I should just use Excel and save time! The problem is that Excel isn’t really easier:
<- 2 a + b This code is generalizable and can be easily adapted to a new problem Process is clear. Output is clear. Show it to anyone and they’ll immediately understand it. Go back 10 years from now, and YOU’LL understand it. 10 years from now: what does this mean (if you can open it)? Also, Excel tables are rigid. Move a cell and… Worst part: Very hard to error-check
for the job • BAD: • “I’m not familiar with X so I’m not going to try it.” • GOOD: • “I am aware of X, and have determined it is not the best course of action because Y”
of 1 > X # What is the value of X? [1] 1 R is a programming language In the console: Anything after a # is a “comment” and is not run R is case sensitive. X and x are two different variables Standard practice in R is to assign value with <-
of 1 > X == 2 # Is X equal to 2? [1] FALSE > X <- 1 # Define X, give it value of 1 > X != 2 # Is X NOT equal to 2? [1] TRUE Logical operators: < > == >= <= Not: ! And: & or: | Exclusive or: xor(x, y)
the computer to load data into memory, manage and manipulate data, perform operations, make figures etc. • Write code for readability and repeatability • Being super efficient less important than being clear (with small to medium- data) • Use lots of comments • 10 years from now, will I understand my code?
operations number_of_fish <- 1 fishname <- “Salmon” #Note the quotation marks number_of_fish + number_of_fish #what does this give? Fishname + number_of_fish #now what? You can perform operations, but the operations have to make sense
A num Retain meta-data! Use comments, or keep a text file with the R script. Note things like: Units! Good: catch_rate Bad: The_number_of_lobs ters_that_I_caught _during_my_study Be concise Good: dollar_value Bad: dolar_value doller_value d0llar_value Avoid typos (seriously!)
we specify number of rolls Die1 <- sample(1:6, 10, replace = T) Die2 <- sample(1:6, 10, replace = T) plot(Die1+Die2) #Basic plotting #command – more later #Gives cumulative dice value # across ten rolls Let’s roll two dice ten times, and plot the results. First, create the variables: !
First, create the variables: Die1 <- 0 Die2 <- 0 NumRolls <- 10 #Here, we specify number of rolls Die1 <- sample(1:6, 10, replace = T) Die2 <- sample(1:6, 10, replace = T) values from 1 to 6 inclusive Do it NumRolls (i.e. 10) times Sample with replacement Important: Use ? to get help ?sample
we specify number of rolls Die1 <- sample(1:6, 10, replace = T) Die2 <- sample(1:6, 10, replace = T) plot(Die1+Die2) #Basic plotting #command – more later #Gives cumulative dice value # across ten rolls Let’s roll two dice ten times, and plot the results. First, create the variables:
more than one type of data Die1 [1] 3 1 2 5 2 2 5 4 5 1 Die2 [1] 5 2 3 2 2 2 4 5 5 3 We have two vectors (sequence of values of same data type): We want: RollNum DiceName Score 1 Die1 3 1 Die2 5 2 Die1 1 2 Die2 2 Number Number String Also:
5 1 Die2 [1] 5 2 3 2 2 2 4 5 5 3 RollNum <- c(1:10) # Let’s make a counter for each roll combine values from one TO ten into a vector called RollNum RollNum [1] 1 2 3 4 5 6 7 8 9 10
5 1 Die2 [1] 5 2 3 2 2 2 4 5 5 3 require(tidyr) install.packages(“tidyr”) Take a package from the Internet, called “tidyr,” and download/install it Note the quotation marks Load the package into memory. No more quotation marks. Why? Once the package is loaded, you get access to every function contained in the package! RollNum [1] 1 2 3 4 5 6 7 8 9 10
Die1:Die2) Make a new variable called DiceData_long Apply the “gather” function… it GATHERS columns into rows See : https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf Do it to DiceData Make a new column called DiceName… Make a new column called Score… … and fill DiceName and Score with everything present in columns Die1 to Die2 (inclusive) from DiceData
DiceDat_long$DiceName) Problem: R often gives meaningless error messages. Always start by ruling out easy stuff. Is there a typo? Check capitalization. Check brackets. Check commas. If something is truly weird, close and re-open R and re-run code. Next, check the data types
each column of DiceData_long https://www.r-bloggers.com/basic-data-types-in-r/ There are several data types in R: DiceName is a ‘character’, or a vector that includes text (i.e. not a number). Solution: Turn DiceName into a FACTOR so we can work with it http://adv-r.had.co.nz/Data-structures.html
vector that has levels described with characters A string of characters that has no numerical value vs Therefore, when we tried: It made no sense from the computer’s perspective. You can’t plot a number against a bunch of text!
includes everything that has been loaded into memory, including variables, packages, functions, etc. Sometimes packages clash, functions you write may cause problems. Always best to start with a fresh workspace. Exception: If you’re working with a massive dataset that takes a long time to analyze. Stock assessment people may encounter this.
got them from an actual experiment. We probably typed them into Excel. Let’s bring those data into R In Excel… type it all in. Save as CSV. • Make sure there are no weird data types • Remember it’s case sensitive • Don’t use commas. CSV means COMMA SEPARATED VALUES. If you put in a comma, R will read it as a new column • We have entered it in long format, so less data manipulation needed • Note: There are packages that allow you to bring data in from an .XLS. Use with caution
data into R Note: By default, read.csv pulls text data in as a factor right away. Pro: Most stats are done w/factors Con: error correction must be done on characters To disable: read.csv(“DiceData.csv”, stringsAsFactors = FALSE) Score Score
Mathematical and logical operators • Some basic R commands: • Using c() and colon – e.g. c(1:10) means 1,2,3,4,5,6,7,8,9,10. c(“cat”, “dog”) makes vector of length 2 with values “cat”, “dog” • How to load packages (tidyr) • How to get help (with ?) • The basic plot command. A few other commands (sapply) • Reading data into R