Day 1: R Basics and data cleaning
Hands-on tutorial using the Global Change Factors dataset (MultiFactor_data.csv)
0. Overview
What you’ll practice today
Loading a CSV in R (several ways)
Inspecting and subsetting data (base R and
dplyrpackage)Basic Data cleaning (handling missing values)
Export data
Dataset:
MultiFactor_data.csv(Download the dataset from the blackboard).
Dataset structure:
Global change factors (treatments):
Column
remark= treatment labels (CT= control,I= Insecticide,A= Antibiotic,P= PFAS,SU= Surfactant,H= Herbicide,S= Salinity,M= Microplastic,D= drought,C= Copper,FU= Fungicide,L= Lithium,N= Nitrogen deposition)
Soil response variables (measurements):
WSA(water stable aggregates),Decom(soil decomposition rate),PH(soil pH),actv_ace(N-acetyl-glucosaminidase activity),actv_cello(cellulase activity),actv_gluco(β-glucosidase activity),actv_phos(phosphatase activity).
1. Setup: working directory & packages
# install.packages("tidyverse")
# Load core packages
library(ggplot2)
library(dplyr)
library(tidyverse)Setup your working directory. (create a folder
ABV_course on your desktop, and set up you working
directory to this folder) (also create R script files in the same
folder)
Session->Set Working Directory->Choose Directory
Place the dataset MultiFactor_data.csv in your working
file (working directory) and import the dataset into R.
Importing dataset
2. R language basics (10 minutes quick tour)
# Assignment
x <- 5
y <- c(1, 3, 5, 7) # numeric vector
z <- c("CT","D","S") # character vector
# Sequences and repetition
seq(0, 1, by = 0.2)## [1] 0.0 0.2 0.4 0.6 0.8 1.0
## [1] "CT" "CT" "CT"
## [1] 4
## [1] 2.581989
## [1] 16
## [1] 4
## [1] FALSE FALSE TRUE TRUE
## [1] 5 7
## [1] FALSE FALSE TRUE FALSE
## [1] 2.333333
# Lists & data frames
lst <- list(a = 1:3, b = c("x","y"))
df <- data.frame(grp = c("CT","D","CT"), WSA = c(55, 48, 60))
str(df)## 'data.frame': 3 obs. of 2 variables:
## $ grp: chr "CT" "D" "CT"
## $ WSA: num 55 48 60
Rule of thumb: vectors are the basic building block; data frames/tibbles are just named columns of equal length.
3. Inspect the data
# Structure and column names
str(dat)
# Treatment codes present (e.g., "CT" control, "D" drought, etc.)
unique(dat$remark)
dim(dat) # rows x cols
names(dat) # column names
head(dat, 3) # first 3 rows
Tip: If you want to rename your column, try this command:
names(dat)[names(dat)=="remark"] <- "Treatment"(renameremarkcolumn asTreatment) Use Chat gpt to understand the command
4. Subset & merge (base R and dplyr command)
We’ll extract control (CT) and drought (D) treatment as a running example.
5. Basic cleaning & type fixes
Make remark a factor; ensure Lv is
integer
Check missingness for key numeric variables
## WSA Decom PH actv_ace
## 1 6 10 2 49
6. Write data to disk
# CSV
write_csv(dat_imp, file.path(folder, "CombinedData_cleaned.csv"))