Introduction to R Programming - Lesson 2

发布于:2025-08-19 ⋅ 阅读:(10) ⋅ 点赞:(0)

This tutorial covers advanced data structures in R, focusing on lists, missing values, and data structure operations.

1. Lists

Lists are the most flexible data structure in R, capable of containing elements of different types (vectors, matrices, data frames, even other lists).

Creating Lists

# Create a list containing various elements
patient <- list(
  name = "John Doe",
  age = 45,
  test_results = c(120, 85, 92),
  is_sick = FALSE,
  medications = c("Aspirin", "Lisinopril")
)

# Print the list
patient

# Check the structure
str(patient)

Accessing List Elements

# Using dollar sign notation (simplest for named elements)
patient$name
patient$age

# Using double brackets (returns the element itself)
patient[[1]]  # First element
patient[["test_results"]]  # By name

# Using single brackets (returns a sublist)
patient[1:2]  # First two elements as a new list
patient["medications"]  # Single element as a sublist

Modifying Lists

# Add a new element
patient$gender <- "Male"

# Modify an existing element
patient$age <- 46

# Remove an element
patient$is_sick <- NULL

# View the modified list
str(patient)

Practice: Create a nested list

# Create a nested list representing a research study
study <- list(
  study_id = "STUDY-2023-001",
  start_date = "2023-01-15",
  participants = list(
    list(id = 1, name = "Alice", scores = c(85, 92, 78)),
    list(id = 2, name = "Bob", scores = c(76, 88, 90))
  )
)

# Extract Bob's second score
study$particip

网站公告

今日签到

点亮在社区的每一天
去签到