Summary by TwinMind · 32m 33s
Summary by TwinMind · 32m 33s

Introduction to R, Part I: Interface and Data Structures

Introduction to R, Part I: Interface and Data Structures

MIT OpenCourseWare

MIT OpenCourseWare

R Introduction and Advantages

  • R is free coding language used in scientific areas (ecology, climate sciences)
  • Key advantages:
    • Free of charge, downloadable with internet connection
    • Collaborative - code sharing through R platform and alternative sources
    • Repeatable/shareable - scripts can be published with research
    • Comprehensive workflow - handles data cleaning, statistical processes, GIS mapping
    • Visualization capabilities - produces quality plots for research communication

RStudio Interface Overview

  • Four main panels in RStudio interface:
    • Script panel: where code is written and orders are listed
    • Console panel: displays executed commands and results
    • Environment panel (top right): stores created data and objects
    • Files/Plots/Packages/Help panel (bottom right): file navigation, plot display, package management, help documentation
  • Interface customization available (background colors, fonts)
  • Help function: FN + F1 to access function documentation

Basic R Operations and Variable Assignment

  • R functions as "big calculator"
  • Assignment operator: arrow sign (<-)
  • Variable creation: x <- 2
  • Code execution methods:
    • Point at line + Command + Return (Mac)
    • Highlight code + click Run button
  • Comments: created using # symbol (appear in gray, not executed)
  • Variable overwriting: reassigning values replaces previous values
  • Mathematical operations: addition, exponential functions (exp())
  • Variable removal: rm() function

Logical Operations

  • Comparison operators:
    • Greater than: >
    • Less than: <
    • Equal to: == (two equal signs)
    • Not equal to: !=
  • Returns TRUE/FALSE boolean values

Data Types

  • Numeric data types: numbers with decimals
  • Integer data types: whole numbers without decimals
  • Character data types: text/words (must use quotes and c() function)
  • Class identification: class() function
  • Data type checking: is.integer() function
  • Type conversion: as.integer() function forces numeric to integer

Vectors

  • Multiple values assigned to single variable using c() function
  • Creation syntax: vector_name <- c(value1, value2, value3)
  • Numeric vectors: c(1, 2, 3)
  • Character vectors: c("Monday", "Tuesday", "Wednesday")
  • Vector operations:
    • rep() function: repeats values (rep(22, 20) repeats 22 twenty times)
    • Sequence creation: 1:100 creates numbers 1 to 100
    • seq() function: creates sequences with specified intervals (seq(1, 100, by=2))
  • Vector indexing:
    • Single element: vector[20] accesses 20th position
    • Multiple elements: vector[c(10, 12)] accesses positions 10 and 12
    • Element removal: vector[-3] removes 3rd element
  • Random data generation:
    • sample() function: random selection from range
    • rnorm() function: normal distribution random data
  • Vector analysis functions:
    • length(): returns vector length
    • summary(): provides min, max, quartiles, median, mean
    • sum(): adds all vector elements
    • min(), max(): minimum and maximum values
  • Visualization: hist() creates histograms

Matrices

  • Two-dimensional data structure (rows and columns)
  • Only numerical variables allowed (no characters)
  • Creation: matrix() function specifying data, rows, columns
  • Example: matrix(vector, nrow=10, ncol=5)
  • Data must fit matrix dimensions (warnings for mismatched sizes)
  • Column naming: colnames() function assigns names
  • Matrix navigation:
    • [row, column] indexing system
    • [4, 3]: specific cell (row 4, column 3)
    • [4, ]: entire row 4
    • [, 3]: entire column 3
  • Column selection: matrix[, c(1, 3)] selects columns 1 and 3

Data Frames

  • Evolution of matrices allowing mixed data types (numbers and characters)
  • Built-in datasets available in R (mtcars example)
  • Data frame operations:
    • length(): number of columns
    • nrow(): number of rows
    • colnames(): column names
    • Same indexing as matrices [row, column]
  • Column access using $ operator: dataframe$columnname
  • Auto-complete feature for column names after $ symbol

Lists

  • Master data structure containing all other data types
  • Can store vectors, matrices, data frames together
  • Creation: list() function with multiple data structures
  • Hierarchical storage with expandable view in RStudio
  • Element access using [[index]] notation
  • Ultimate flexibility for mixed data storage

Practice Exercises

  • Exercises provided to practice concepts covered
  • Additional internet resources recommended for further learning
  • R community provides extensive online documentation and tutorials