📊

Beginner's Guide to R Programming

Mar 24, 2025

R Programming 101

Introduction to R

  • R is a programming language used for statistical and data analysis.
  • Ideal for beginners; this session targets individuals who have never used R before.
  • Objective: By the end of this lesson, you'll feel comfortable with the basics of R.

R Console Overview

  • Source: Area to write your code.
    • Example: 5 + 6 executed with Ctrl + Enter gives 11 in the console.
  • Console: Displays the results of the executed code.

Basic Operations in R

Using R as a Calculator

  • R can perform basic arithmetic operations like a calculator.
    • Example: 5 + 6 results in 11.

Assigning Values

  • Use the <- operator to assign values to variables.
    • Example: a <- 5, b <- 6.
    • Operations can be performed on variables: a + b results in 11.

Functions in R

  • Functions perform specific tasks.
    • Example: sum(a, b) adds variables a and b.

Working with Data

Creating Variables

  • Variables can store more than one value.
    • Example: name <- c("Greg", "Jill"), where c() concatenates values.

Data Frames

  • Combine multiple variables into a data frame.
    • Example: friends <- data.frame(name, age, gender).
    • data.frame() function creates a table-like structure (data frame).

Accessing Data Frame Elements

  • Use the $ operator to access specific variables in a data frame.
    • Example: friends$name.

Subsetting Data Frames

  • Use square brackets [] to subset data.
    • Example: friends[1, ] gets the first row.
    • Example: friends[, 1] gets the first column.

Introduction to Tidyverse

Simplifying Data Manipulation

  • Tidyverse: A collection of R packages for data manipulation.
    • Install using: install.packages("tidyverse").
    • Load with: library(tidyverse).

Using Pipes

  • Use the pipe operator %>% to streamline code.
    • Example: friends %>% select(name, age) %>% filter(age < 50).
    • Simplifies complex data extraction and manipulation.

Conclusion

  • R is user-friendly and intuitive once you understand the basics.
  • Tidyverse enhances R with natural language capabilities.
  • Encouraged to explore more advanced features and packages.

Reminder

  • "Don't ever change, don't do drugs, always do your best." - Closing encouragement from the instructor.