Module 1: Environmental Datasets, Variable Types & R/RStudio Setup
Module 1: Environmental Datasets, Variable Types & R/RStudio Setup
Deconstructing scientific datasets: observations (rows), variables (columns), tidy data architecture, continuous vs. discrete numerical variables, nominal vs. ordinal categorical factors in R, and environment setup.
1. Deconstructing Scientific Data: Structure, Variables & Observations
In environmental and scientific research, data is collected from field monitoring stations, laboratory assays, satellite sensors, and controlled ecological experiments. To analyze this information in R, data must conform to the Tidy Data paradigm (Wickham, 2014):
The Three Core Rules of Tidy Scientific Data:
- Each variable forms a column: Attributes like Water Temperature (°C), Dissolved Oxygen (mg/L), or Sampling Depth (m) reside in distinct vertical columns.
- Each observation forms a row: A single sampling event, individual specimen, or sensor timestamp occupies exactly one horizontal row.
- Each type of observational unit forms a table: Do not mix stream discharge rates with macroinvertebrate community counts in the same unindexed matrix.
Taxonomy of Scientific Variables in R
Choosing the correct statistical test hinges entirely on identifying whether your dependent (response) and independent (predictor) variables are numerical or categorical:
| Variable Category | Sub-Type | Environmental Example | R Data Type / Class |
|---|---|---|---|
| Numerical (Quantitative) | Continuous | Soil pH (6.42), Heavy Metal Pb (0.045 mg/kg), River flow (m³/s) | numeric / double |
| Numerical (Quantitative) | Discrete (Counts) | Fish species richness (14), Tree seedling count (8), Bacterial colonies | integer |
| Categorical (Qualitative) | Nominal | Land use type (Forest, Agriculture, Urban), River basin name | factor (unordered) |
| Categorical (Qualitative) | Ordinal | Pollution severity index (Low < Moderate < Severe), Erosion risk class | ordered factor |
Hands-On R Script: Setting Up Your Scientific Workspace & Inspecting Data
Run the following code block in RStudio to install the essential environmental data science stack and construct a tidy water quality monitoring tibble:
# 1. Install & Load Tidyverse Environment
# install.packages(c("tidyverse", "readxl", "summarytools"))
library(tidyverse)
# 2. Create a Representative Environmental Monitoring Dataset
river_data <- tibble(
station_id = factor(c("ST_01", "ST_02", "ST_03", "ST_04", "ST_05", "ST_06")),
land_use = factor(c("Upstream_Forest", "Upstream_Forest", "Agriculture", "Agriculture", "Urban_Downstream", "Urban_Downstream")),
sampling_date = as.Date(c("2026-03-01", "2026-03-01", "2026-03-02", "2026-03-02", "2026-03-03", "2026-03-03")),
water_temp_c = c(14.2, 14.5, 17.8, 18.1, 21.4, 22.0),
dissolved_o2 = c(9.8, 9.6, 7.2, 6.9, 4.3, 3.8), # mg/L
nitrate_ppm = c(0.42, 0.48, 5.20, 6.10, 8.40, 9.15),
macroinvert_count = as.integer(c(48, 44, 21, 18, 6, 4))
)
# 3. Inspect Structure & Column Attributes
glimpse(river_data)
summary(river_data)Common Pitfalls & Troubleshooting Checklist
- Java Heap Exhaustion (GATK / Picard): If processes terminate with
java.lang.OutOfMemoryError, set your environment variable explicitly:export _JAVA_OPTIONS="-Xmx16g -Djava.io.tmpdir=/tmp". - Index Timestamp Mismatch: If tools complain that a BAM index is missing or out of date, regenerate with
samtools index -@ 4 sample.bam. - Conda Channel Conflicts: Avoid ABI incompatibilities between bioconda and conda-forge by setting strict priority:
conda config --set channel_priority strict. - Chromosome Naming Contamination: Always verify whether reference FASTA headers use
chr1or1; divergent naming causes silent alignment drops.