Beginner's Guide to R Programming

Aug 12, 2024

R Programming 101 Lecture Notes

Introduction to R Programming

  • Purpose of R: Programming language for statistical and data analysis.
  • Audience: Beginners with no prior experience in R.
  • Goal: Understand basics of R, comfortable using R for data analysis.

R Environment Overview

Console and Source

  • Source: Area where you write code.
  • Console: Displays outputs, like a sophisticated calculator.

Basic Operations

  • Arithmetic: R can perform basic calculations, e.g., 5 + 6 = 11.

Assigning Values

Variables

  • Assignment Operator: Use <- to assign values.
    • Example: a <- 5, b <- 6.
  • Using Variables: a + b will result in 11.

Functions in R

Sum Function

  • Using Functions: Functions take arguments within brackets.
    • Example: sum(a, b) results in 11.

Working with Multiple Values

Vectors

  • Creating Vectors: Use c() to concatenate values.
    • Example: name <- c('Greg', 'Jill').

Data Structures

Data Frames

  • Creating Data Frames: Combine vectors into a data frame.
    • Example: friends <- data.frame(name, age, gender).
  • Environment: Data frames and variables show up in the environment pane.

Subsetting Data

Accessing Data

  • Dollar Sign Operator: To access specific variables in a data frame.
    • Example: friends$name.

Subsetting with Square Brackets

  • Rows and Columns: Use [rows, columns] to subset data.
    • Example: friends[1, ] gives the first row.
  • Conditional Subsetting: Use conditions to extract parts of data.
    • Example: friends[friends$age < 50, 1:2].

Introduction to Tidyverse

Packages

  • Tidyverse: A collection of R packages for data manipulation.
  • Installing: install.packages('tidyverse').
  • Loading: library(tidyverse).

Simplifying Operations

  • Pipe Operator: %>% simplifies chaining commands.
    • Example: friends %>% select(name, age) %>% filter(age < 50).

Conclusion

  • Ease of Use: R is user-friendly with practice.
  • Future Learning: Tidyverse and other packages make R intuitive and expand its capabilities.

Final Remarks

  • Encouragement to continue learning and exploring R.
  • Motivating closing message: Stay curious and persistent.