#--------------------------#
# MB5370: Techniques in Marine Science 1
# Programming Fundamentals
# Nur Imannina Binti Adam
# 15/09/2026
#--------------------------------------#
# Worksop 01. Introduction ####
# R syntaz and working in scripts ####
#This section introduces us to R by running simple calculation inside a script.
## Basic arithmetic (e.g. wrangling sea surface temperatures)
24.5 + 1.2
## [1] 25.7
32.0 / 4
## [1] 8
# Indexed outputs
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
# Incomplete lines of code
6*
2
## [1] 12
6 * 2
## [1] 12
#Functions and arguments
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 obect to specify argument
## [1] 25.76
#?round #go to help
args(round) # use args in the Console
## function (x, digits = 0, ...)
## NULL
# Objects and Assignment
## This section focuses on understanding how data is stored in R and why that matters.
# Saving a single value
coral_count <- 42
# Saving a vector of multiple fish lengths (in mm)
fish_lengths <- c(124, 152, 98, 221, 146)
# Manipulating objects
coral_count +1
## [1] 43
coral_count + coral_count
## [1] 84
Coral_Count <-1
coral_count + Coral_Count
## [1] 43
# Object naming rules
#01_age <- 25 # starts with a number
#!_age <- 25 # no special symbols
#coral count <- 25 # no spaces
'coral count' <- 25 # spaces used but with back ticks
# Debugging code
# Field survey data
quadrat_area_m2 <- 0.25
number_of_quadrats <- 16
total_area_surveyed <- quadrat_area_m2 * number_of_quadrats
# Lets print out the result
print(total_area_surveyed)
## [1] 4
# Installing and loading tidyverse
library(tidyverse) #load into current session
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'tibble' was built under R version 4.5.3
## Warning: package 'tidyr' was built under R version 4.5.3
## Warning: package 'readr' was built under R version 4.5.3
## Warning: package 'purrr' was built under R version 4.5.3
## Warning: package 'dplyr' was built under R version 4.5.3
## Warning: package 'stringr' was built under R version 4.5.3
## Warning: package 'lubridate' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.4 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Checking structures
# Assiggn variable values
site_name <- "Heron_Island"
transect_depth_m <- 12.5
bleaching_present <- TRUE
# Check using function class ()
class (site_name)
## [1] "character"
class(transect_depth_m)
## [1] "numeric"
class(bleaching_present)
## [1] "logical"
# Check using function str ()
str(site_name)
## chr "Heron_Island"
str(transect_depth_m)
## num 12.5
str(bleaching_present)
## logi TRUE
# Rounding numbers
# Tracking the age of an old-growth Porites coral colony
years_old <- 25.765
# Clean this up for our summary report
round(years_old, 1)
## [1] 25.8
# Manipulating outputs
# Make variables
years_old <- 25.765
rounded_age <-round(years_old, 1)
# Combine text and data variables
paste("Average colony age is", rounded_age, "years old")
## [1] "Average colony age is 25.8 years old"
# Data structures
fish_lengths <- c(124, 152, 98, 221, 146)
coral_spp <- c("Porites", "Acropora", "Montastrea")
class(fish_lengths)
## [1] "numeric"
class(coral_spp)
## [1] "character"
notes <- c("Acropora", 27.5, TRUE)
notes <- list ("Acropora", 27.5, TRUE)
notes
## [[1]]
## [1] "Acropora"
##
## [[2]]
## [1] 27.5
##
## [[3]]
## [1] TRUE
notes[[1]]
## [1] "Acropora"
# Data frames and tibbles
my_dataframe <- data.frame(no = c(1,2,3), c("Plectropomus", "Scarus", "Pomacentrus"), c(TRUE, FALSE, TRUE))
my_dataframe
## no c..Plectropomus....Scarus....Pomacentrus.. c.TRUE..FALSE..TRUE.
## 1 1 Plectropomus TRUE
## 2 2 Scarus FALSE
## 3 3 Pomacentrus TRUE
str(my_dataframe)
## 'data.frame': 3 obs. of 3 variables:
## $ no : num 1 2 3
## $ c..Plectropomus....Scarus....Pomacentrus..: chr "Plectropomus" "Scarus" "Pomacentrus"
## $ c.TRUE..FALSE..TRUE. : logi TRUE FALSE TRUE
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
## $ c..Plectropomus....Scarus....Pomacentrus..: chr "Plectropomus" "Scarus" "Pomacentrus"
## $ c.TRUE..FALSE..TRUE. : logi TRUE FALSE TRUE
# Set working directory
#setwd("c:/Users/DrShark/Desktop/MyDocuments/Class2026/Finat_Data/Data/")
# Getting set up with R projects
# MB5370_Module_1_Coding.Rproj
# data/ <- For raw, unedited data files (CSVs, shapes, etc.)
# code/ <- For dirty/heavy data manipulation code
# output/ <- For processed tables, figures, and saved objects
# docs/ <- For your rendered write-ups and final documents