Introduction

This file covers the absolute basics of R programming, such as syntax, functions, and types of data.

download and install then comment out so it doesn’t run again

#install.packages("tidyverse") 
#install.packages("redlistr") 
#install.packages("terra") 

load packages

library("ggplot2")
library("tidyr")
# top 2 are included in tidyverse library
library("tidyverse") 

Help options

?round # go to help
args(round) # use args in the Console
## function (x, digits = 0, ...) 
## NULL

Basics

2+1
## [1] 3
1:30
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30
6 * 2
## [1] 12
#6 % 2
Age <- 25 
first_name <- 'Bill'
Age + 1
## [1] 26
Age + Age 
## [1] 50
x = 15 + 25.1 + 20.25
y = 5
x + y
## [1] 65.35

Naming Errors:

01_age <- 25 # starts with a number !_age <- 25 # no special symbols age bob <- 25 # no spaces age bob <- 25 # no spaces, but with back ticks

Functions

years_old <- 25.7
round(years_old) # rounds up
## [1] 26
floor(years_old) # rounds down
## [1] 25
years_old <- 25.765
round (years_old, 2) # comma after the object to specify argument to 2 decimal places
## [1] 25.76

Paste Function

years_old <- 24
Ryan <- paste("Ryan is", years_old)

Misconceptions (variables in programs do not work the same way as they do in spreadsheets)

grade <- 55
total <- grade + 10
print (total)
## [1] 65
grade <- 90
print (total) # value of total in a spreadsheet will be 100, but in programming a variable holds the value it was assigned (65)
## [1] 65
total <- grade + 10
print (total) # executed in the way it was defined
## [1] 100

Debugging errors

p <- 2
z <- 5
out <- p * z  # What should the value of out be?
print (out) # What is the value of out? Is it the same as expected?
## [1] 10
#out <- p * a # undefined variable.
## > Error: object 'a' not found
out <- p * z

Example: Lacks commas between objects

my_quiz <- c(“uno”, “dos”, “tres”, “cuatro” “cinco”) print (my_quis) #Quiz misspelled str(my_quiz) len(my_quiz) #Len should be length

Fixed Code

my_quiz <- c("uno",
             "dos",
             "tres",
             "cuatro",
             "cinco")
print (my_quiz)
## [1] "uno"    "dos"    "tres"   "cuatro" "cinco"
str(my_quiz)
##  chr [1:5] "uno" "dos" "tres" "cuatro" "cinco"
length(my_quiz)
## [1] 5

Testing if data is numeric or a character

x <- 1
is.character(x)
## [1] FALSE
is.numeric (x)
## [1] TRUE

Types of Data

#Vector
y <- c(1, 2, 3)
z <- c("Sarah", "Tracy", "Jon")

class(y) 
## [1] "numeric"
class(z)
## [1] "character"
#Lists (values inside can be of different types)
x <- list(1, "a", TRUE)
x
## [[1]]
## [1] 1
## 
## [[2]]
## [1] "a"
## 
## [[3]]
## [1] TRUE
x[[2]] #Retrieve 2nd value
## [1] "a"
#Insted of importing .csv files you can make your own data frames by adding vectors and giving them column names
my_dataframe <-  data.frame (no = c(1,2,3), name = c("Tracey", "John", "Pete"), pass = c(TRUE, FALSE, TRUE))
my_dataframe
##   no   name  pass
## 1  1 Tracey  TRUE
## 2  2   John FALSE
## 3  3   Pete  TRUE
str(my_dataframe)
## 'data.frame':    3 obs. of  3 variables:
##  $ no  : num  1 2 3
##  $ name: chr  "Tracey" "John" "Pete"
##  $ pass: logi  TRUE FALSE TRUE
#Set 1st column as categorical rather than numeric 
my_dataframe$no = as.factor(my_dataframe$no)
str (my_dataframe)
## 'data.frame':    3 obs. of  3 variables:
##  $ no  : Factor w/ 3 levels "1","2","3": 1 2 3
##  $ name: chr  "Tracey" "John" "Pete"
##  $ pass: logi  TRUE FALSE TRUE

Class : We want to take a .csv dataset representing counts from a BRUVs survey and turn it into a plot of the abundance of a single species. What major steps - the main tasks you would need to do using programming, might be required to get from a structured .csv table to a plot of a single species? Record your answers in your script using numbered comments.

1. Read the .csv dataset into R. etc.

2. Sort the data as needed and define NA

3. Plot the data selected with the plot function and edit abundance plot as desired