Lesson 4
Working with Vectors
Exploring Vectors in R

Welcome back! In our previous lessons, you learned about strings and basic numerics, including how to assign variables and perform arithmetic operations. Building on that foundation, we now turn our attention to vectors in R.

What You'll Learn

In this lesson, you will discover how to:

  • Create numeric and character vectors
  • Access specific elements within these vectors

What are Vectors?

A vector in R is a simple data structure that contains elements of the same type. Think of a vector as a list of items (like numbers or words) that you can store and manipulate together. For example, you can have a vector of numbers to represent ages or a vector of words to represent names.

Here's a quick preview of some of the stuff that we'll cover:

R
1# Create a numeric vector 2numeric_vec <- c(1, 2, 3, 4, 5) 3print(numeric_vec) # Prints: [1] 1 2 3 4 5 4 5# Create a character vector 6char_vec <- c("one", "two", "three", "four", "five") 7print(char_vec) # Prints: [1] "one" "two" "three" "four" "five" 8 9# Access vector elements 10print(numeric_vec[1]) # Prints: [1] 1 11print(char_vec[2:4]) # Prints: [1] "two" "three" "four"

By the end, you'll have the skills to handle and manipulate vectors in R, which is a crucial aspect of data analysis!

Why It Matters

Vectors enable you to store and manipulate multiple items efficiently. This is vital for data analysis, where you often work with datasets containing numerous entries. For instance, if you're analyzing survey results or financial data, you need to efficiently access and perform operations on these vectors to derive meaningful insights.

Pretty exciting, right? Let's dive into the practice section and start working with vectors in R.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.