Introduction

In R, vectors and lists are fundamental data structures that allow you to store and manipulate collections of data. Understanding how to work with these structures is crucial for effective data analysis and manipulation in R.

Vectors

What is a Vector?

A vector is a basic data structure in R that contains elements of the same type. Vectors can be numeric, character, logical, or any other type, but all elements must be of the same type.

Creating Vectors

You can create vectors using the c() function, which stands for "combine" or "concatenate."

# Numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)

# Character vector
character_vector <- c("a", "b", "c", "d")

# Logical vector
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)

Accessing Vector Elements

You can access elements of a vector using square brackets [].

# Accessing the first element
first_element <- numeric_vector[1]

# Accessing multiple elements
multiple_elements <- numeric_vector[c(1, 3, 5)]

Vector Operations

Vectors support various operations such as arithmetic operations, logical operations, and more.

# Arithmetic operations
sum_vector <- numeric_vector + 2
product_vector <- numeric_vector * 2

# Logical operations
logical_check <- numeric_vector > 3

Practical Example

# Create a numeric vector
scores <- c(85, 90, 78, 92, 88)

# Calculate the mean score
mean_score <- mean(scores)

# Find scores greater than 80
high_scores <- scores[scores > 80]

# Print results
print(mean_score)
print(high_scores)

Lists

What is a List?

A list is a more flexible data structure in R that can contain elements of different types, including other lists.

Creating Lists

You can create lists using the list() function.

# Creating a list
my_list <- list(
  name = "John",
  age = 30,
  scores = c(85, 90, 78),
  is_student = TRUE
)

Accessing List Elements

You can access elements of a list using the $ operator or double square brackets [[]].

# Accessing elements using $
name <- my_list$name

# Accessing elements using [[]]
age <- my_list[["age"]]

# Accessing nested elements
first_score <- my_list$scores[1]

Modifying List Elements

You can modify elements of a list by assigning new values.

# Modifying an element
my_list$age <- 31

# Adding a new element
my_list$city <- "New York"

Practical Example

# Create a list
student <- list(
  name = "Alice",
  age = 25,
  grades = c(88, 92, 79),
  graduated = FALSE
)

# Calculate the average grade
average_grade <- mean(student$grades)

# Update graduation status
student$graduated <- TRUE

# Print results
print(average_grade)
print(student)

Exercises

Exercise 1: Create and Manipulate a Vector

  1. Create a numeric vector named temperatures with the values: 23, 25, 27, 22, 24.
  2. Calculate the average temperature.
  3. Find temperatures greater than 24.

Solution:

# Step 1
temperatures <- c(23, 25, 27, 22, 24)

# Step 2
average_temperature <- mean(temperatures)

# Step 3
high_temperatures <- temperatures[temperatures > 24]

# Print results
print(average_temperature)
print(high_temperatures)

Exercise 2: Create and Manipulate a List

  1. Create a list named employee with the following elements:
    • name: "Bob"
    • age: 28
    • department: "HR"
    • salaries: c(50000, 52000, 54000)
  2. Calculate the average salary.
  3. Update the department to "Finance".

Solution:

# Step 1
employee <- list(
  name = "Bob",
  age = 28,
  department = "HR",
  salaries = c(50000, 52000, 54000)
)

# Step 2
average_salary <- mean(employee$salaries)

# Step 3
employee$department <- "Finance"

# Print results
print(average_salary)
print(employee)

Conclusion

In this section, we covered the basics of vectors and lists in R. Vectors are used to store elements of the same type, while lists can store elements of different types. Understanding these data structures is essential for effective data manipulation in R. In the next section, we will explore matrices and arrays, which are more complex data structures.

© Copyright 2024. All rights reserved