Chapter 29: R Vectors

Part 1: What is a Vector?

vector is a basic data structure in R that contains elements of the same type. The most important characteristic of a vector is that all elements must be the same data type (all numbers, all characters, all logical values, etc.).

The Six Types of Vectors

R has six fundamental types of vectors:

  1. Numeric (double) – decimal numbers

  2. Integer – whole numbers

  3. Character – text strings

  4. Logical – TRUE/FALSE values

  5. Complex – complex numbers

  6. Raw – raw bytes

But for everyday use, you’ll primarily work with numeric, character, and logical vectors.

Part 2: Creating Vectors

The c() Function – Combine

The most common way to create vectors is with the c() function (combine):

r

Sequence Creation

r

Repetition

r

Part 3: Vector Properties

Length and Type

r

Attributes

r

Part 4: Accessing Vector Elements

Indexing (1-based!)

R uses 1-based indexing, meaning the first element is at position 1:

r

The Difference Between [ and [[

r

Part 5: Vector Operations

Arithmetic Operations

r

Recycling

When vectors have different lengths, R recycles the shorter vector:

r

Comparison Operations

r

Mathematical Functions

r

Vectorized Functions

One of R’s greatest strengths is that most functions are vectorized – they operate on entire vectors at once:

r

Part 6: Modifying Vectors

Changing Elements

r

Removing Elements

r

Part 7: Vector Functions and Utilities

Set Operations

r

Sorting and Ordering

r

Unique and Duplicated

r

Sampling and Randomization

r

Part 8: Special Vector Types

Integer Vectors

r

Logical Vectors

r

Character Vectors

r

Factor Vectors (special)

r

Part 9: Missing Values (NA)

r

Part 10: Practical Examples

Example 1: Grade Calculator

r

Example 2: Stock Price Analysis

r

Example 3: Text Analysis

r

Part 11: Common Mistakes and How to Avoid Them

Mistake 1: 0-based Indexing

r

Mistake 2: Forgetting that Vectors are Homogeneous

r

Mistake 3: Using = instead of == in conditions

r

Mistake 4: Forgetting about Recycling

r

Mistake 5: Confusing & and &&

r

Part 12: Performance Tips

Pre-allocation

r

Vectorization is Faster

r

Summary: The Vector Philosophy

Vectors are the foundation of R programming. Master these concepts:

Creating vectors:

  • c() for combining

  • : for sequences

  • seq() for flexible sequences

  • rep() for repetitions

Accessing elements:

  • Single bracket [] with position

  • Logical vectors for conditional access

  • Names for named access

Vector operations:

  • Arithmetic is element-wise

  • Recycling for different lengths

  • Vectorized functions for efficiency

Vector functions:

  • length()typeof()class()

  • sum()mean()sd(), etc.

  • sort()order()rev()

  • unique()table()

  • %in% for membership testing

Key principles:

  1. All elements must be the same type

  2. Indexing starts at 1

  3. Most functions are vectorized

  4. Missing values (NA) propagate

  5. Pre-allocation for speed

Vectors are like the atoms of R – small, fundamental, and when combined, they create everything else in the language. Master vectors, and you’ve mastered the foundation of R!

Would you like me to elaborate on any specific aspect of vectors or explore more advanced vector operations?

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *