Course 1: R Programming Fundamentals
R is a powerful programming language tailored for statistical analysis, data visualization, and data science. This course introduces beginners to R, covering its environment, syntax, data types, operators, control flow, and functions. Designed for those new to programming or transitioning from other languages, it provides a foundation for advanced data analysis. Over one week, daily lessons build skills through practical examples and exercises, culminating in a solid understanding of R fundamentals.
Objective: By the end of the course, students will be able to write R scripts, manipulate data using vectors, apply control flow, create reusable functions, and set up a workflow for data analysis tasks.
Scope: The course covers R installation, basic syntax, variables, data types, operators, vectors, control structures, and functions, preparing learners for intermediate R programming and data science applications.
Day 1: Introduction to R Programming
Introduction: R is a powerful programming language tailored for statistical analysis, data visualization, and data science. It is widely used in both academic research and industry, supported by a vast ecosystem of packages and a strong global community.
Learning Objective: By the end of this session, learners will be able to set up the R environment, write basic R code, and understand fundamental R data types.
Scope of the Lesson: Today's focus includes: Installing R and RStudio; Running R scripts and interacting with the R console; Understanding basic syntax: assignments and comments; Recognizing key data types: numeric, character, and logical.
Background Information: History: R was developed in 1993 by Ross Ihaka and Robert Gentleman at the University of Auckland. Environment: RStudio is the most popular Integrated Development Environment (IDE) for R, offering features like project management, visual debugging, and powerful plotting tools. Syntax Basics: Assignments: Use either <- or = (though <- is preferred in R style guides); Comments: Begin with a # symbol and are ignored during code execution. Data Types: Numeric: Numbers, e.g., 5, 3.14; Character: Strings, e.g., "Hello World"; Logical: Boolean values, TRUE or FALSE. The openness of R (licensed under GPL) and its Comprehensive R Archive Network (CRAN) make it a highly extensible language.
Simple Code Examples:
# Assignment x <- 5 # Comment # This is a comment explaining the code # Data Types num_value <- 3.14 # Numeric char_value <- "Hello!" # Character log_value <- TRUE # Logical # Printing values print(x) print(char_value) print(log_value)
Explanation of the Example Code: Assignment: x <- 5 assigns the value 5 to the variable x. Comment: # This is a comment is a note for humans reading the code. Printing: print() outputs the value to the console for review.
Supplemental Information: Understanding the R environment and basic syntax lays the foundation for more complex tasks like data manipulation and statistical modeling later in the course.
Resources:
- Cheatsheet: x <- 5 # Assignment; # This is a comment
- Video: R for Beginners by DataCamp
- Book: R for Data Science by Hadley Wickham & Garrett Grolemund (2016)
Day 2: Variables and Data Types
Introduction: In R, variables serve as containers for storing data used in analysis. Understanding data types—such as numeric, character, and logical—is fundamental to performing data manipulation and building reliable code.
Learning Objective: By the end of this session, learners will be able to create variables, recognize R’s key data types, perform type conversions, and handle basic input and output.
Scope of the Lesson: Today's focus includes: Creating and naming variables in R; Identifying and working with major data types: numeric, integer, character, and logical; Converting between types using R functions; Performing basic input and output operations.
Background Information: Variable Creation: Variables are created using <- or =. Example: x <- 5. Dynamic Typing: R determines a variable’s type at runtime; no explicit type declarations are needed. Common Data Types: Numeric (Double): Decimal numbers (e.g., 3.14); Integer: Whole numbers (e.g., 5L); Character: Text strings (e.g., "Data Science"); Logical: Boolean values (TRUE, FALSE). Type Conversion Functions: as.numeric(), as.character(), as.logical(), as.integer() are used to change variable types when needed. Input/Output: print() displays values; readline() captures user input from the console. Naming Conventions: Names should be descriptive (my_var, not x1); Avoid using reserved R keywords (e.g., if, else); Use letters, numbers, underscores, and dots—but variable names cannot start with a number.
Simple Code Examples:
# Creating Variables num_var <- 5.2 # Numeric int_var <- as.integer(5) # Integer char_var <- "Hello R" # Character log_var <- TRUE # Logical # Type Conversion converted_num <- as.numeric("123") converted_char <- as.character(456) # Output print(num_var) print(paste("Converted number:", converted_num)) # Basic Input user_input <- readline(prompt = "Enter your name: ") print(paste("Hello,", user_input))
Explanation of the Example Code: Variable Creation: Creates four different types of variables. Type Conversion: Changes character to numeric and vice versa. Output: print() displays the variables in the console. Input: readline() prompts the user and captures their response.
Supplemental Information: Mastering variables and data types lays the groundwork for working with complex structures like vectors, lists, and data frames later in the course.
Resources:
- Cheatsheet: as.numeric("5") # Type conversion; print(x) # Output to console
- Video: R Variables and Data Types by R Programming 101
- Book: R for Data Science by Hadley Wickham & Garrett Grolemund (2016)
Day 3: Operators and Vectors
Introduction: Operators and vectors form the backbone of data manipulation in R. Mastery of these concepts allows for efficient and intuitive data analysis, leveraging R’s strength in vectorized operations.
Learning Objective: By the end of this session, learners will be able to use arithmetic, comparison, and logical operators, create and manipulate vectors, and perform vectorized operations in R.
Scope of the Lesson: Today's topics include: Using arithmetic, comparison, and logical operators; Creating vectors with consistent data types; Indexing and modifying vector elements; Applying operations across entire vectors using R’s vectorized computing model.
Background Information: Operators in R: Arithmetic: +, -, *, /, ^ (e.g., 2^3 equals 8); Comparison: ==, !=, >, <, >=, <= (e.g., 5 > 3 returns TRUE); Logical: & (AND), | (OR), ! (NOT) (e.g., x > 0 & y < 10). Vectors: Created using the c() function (combine); Must contain elements of the same type; Example: nums <- c(1, 2, 3, 4) creates a numeric vector; R is 1-indexed, meaning indexing starts at 1: nums[1] returns the first element. Vectorized Operations: Operations like addition, subtraction, and logical comparisons apply element-wise automatically; Example: nums + 2 adds 2 to every element in nums; This approach leads to more efficient and readable code compared to traditional loops.
Simple Code Examples:
# Operators a <- 5 b <- 3 sum <- a + b # Arithmetic is_equal <- a == b # Comparison logical_check <- a > 0 & b < 10 # Logical # Vectors vec <- c(10, 20, 30, 40) # Indexing first_element <- vec[1] # Vectorized Operations vec_plus_five <- vec + 5 vec_times_two <- vec * 2 vec_squared <- vec^2
Explanation of the Example Code: Operators: Demonstrates basic arithmetic, comparison, and logical checks. Vectors: Shows how to create a vector and access specific elements. Vectorized Operations: Highlights how R automatically applies operations to all elements.
Supplemental Information: Understanding vector behavior and vectorized operations is crucial because many R functions and packages are optimized with these assumptions in mind.
Resources:
- Cheatsheet: c(1, 2) # Create a simple vector; x > 0 & y < 10 # Logical comparison
- Video: R Vectors and Operators by DataCamp
- Book: R for Data Science by Hadley Wickham & Garrett Grolemund (2016)
Day 4: Control Flow
Introduction: Control flow structures such as if, else, and loops empower R scripts to make decisions and repeat tasks, enabling dynamic and responsive programming essential for real-world data analysis.
Learning Objective: By the end of this session, learners will be able to write conditional statements, use loops to automate repetitive tasks, and control the flow of execution in R scripts.
Scope of the Lesson: Today's topics include: Using if, else, and ifelse() for conditional logic; Writing for loops and while loops for iteration; Controlling loops with break and next.
Background Information: Conditional Statements: if: Executes a block of code if a condition is true; else: Executes alternative code if the if condition is false; ifelse(): A vectorized function for element-wise conditional operations. Loops: for: Iterates over elements in a sequence or vector; while: Repeats a block of code while a condition remains true. Loop Control: break: Exits the loop immediately; next: Skips the current iteration and moves to the next.
Simple Code Examples:
# Conditional Statements x <- 3 if (x > 0) { print("Positive") } else { print("Non-positive") } # Vectorized Conditional nums <- c(-2, 0, 5, 8) labels <- ifelse(nums > 0, "Positive", "Non-positive") print(labels) # For Loop for (i in 1:5) { print(paste("Number:", i)) } # While Loop with break and next count <- 1 while (count <= 10) { if (count == 6) break # Stop the loop when count is 6 if (count %% 2 == 0) { # Skip even numbers count <- count + 1 next } print(count) count <- count + 1 }
Explanation of the Example Code: Conditional Statements: Basic decision-making logic. Vectorized ifelse: Assigns labels based on numeric conditions across a vector. Loops: Automate repetitive tasks. break and next: Fine-tune loop behavior by exiting early or skipping iterations.
Supplemental Information: Mastering control flow enables R programmers to handle more complex data workflows, simulations, and automation tasks with precision.
Resources:
- Cheatsheet: if (x > 0) { ... }; for (i in vec) { ... }
- Video: R Control Flow by R Programming 101
- Book: R for Data Science by Hadley Wickham & Garrett Grolemund (2016)
Day 5: Functions in R
Introduction: Functions are essential building blocks in R programming, allowing you to create reusable code snippets that simplify calculations, automate repetitive tasks, and enhance clarity in data analysis projects.
Learning Objective: By the end of this session, learners will be able to define functions, pass parameters (with or without defaults), handle return values, and understand variable scope inside functions.
Scope of the Lesson: Today's topics include: Defining functions using function(); Using parameters and default arguments; Returning results from functions; Understanding local vs. global variable scope (lexical scoping).
Background Information: Function Definition: Functions are created with the function() keyword. Parameters and Default Arguments: Parameters accept input data; Defaults can be set for parameters if no argument is provided. Return Values: R automatically returns the result of the last evaluated expression if return() is not explicitly used. Variable Scope (Lexical Scoping): Variables created inside functions are local by default; They don’t affect variables outside the function unless specifically assigned using <<-.
Simple Code Examples:
# Defining a simple function square <- function(x) { return(x^2) } square(5) # 25 # Function with default parameter multiply <- function(x, y = 2) { x * y } multiply(3) # 6 multiply(3, 4) # 12 # Lexical scoping example z <- 10 add_to_z <- function(x) { z <- 5 # Local z return(x + z) } add_to_z(3) # Returns 8, not 13 print(z) # Still 10 (global z unchanged)
Explanation of the Example Code: square(): Multiplies a number by itself. multiply(): Demonstrates a function with an optional/default argument. add_to_z(): Shows how local variables inside functions don't overwrite global variables unless using <<-.
Supplemental Information: Mastering functions not only promotes reusable, readable code but also lays the foundation for writing modular and scalable R programs.
Resources:
- Cheatsheet: my_func <- function(x) return(x + 1)
- Video: R Functions by DataCamp
- Book: R for Data Science by Hadley Wickham & Garrett Grolemund (2016)
Daily Quiz
Practice Lab
Select an environment to practice coding exercises.
Exercise
Download the following files to support your learning:
Grade
Day 1 Score: Not completed
Day 2 Score: Not completed
Day 3 Score: Not completed
Day 4 Score: Not completed
Day 5 Score: Not completed
Overall Average Score: Not calculated
Overall Grade: Not calculated
Generate Certificate
Click the button below to generate your certificate for completing the course.