AI, machine learning and deep learning · Introductory

R data structures

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

R data structures are specialized containers for statistical analysis, with vectors serving as the fundamental building block for ordered, homogeneous data. Factors extend vectors to efficiently handle categorical variables by storing unique labels once, while other structures like lists, arrays, matrices, and data frames manage more complex data management tasks.

Intuition

Think of R's data structures as a set of specialized containers designed for statistical analysis. The vector is the core building block, acting like a single row of lockers where every item must be of the same type, such as all integers or all characters. This homogeneity is crucial for performance and consistency. When you need to handle categories, like gender or diagnosis status, you use a factor. A factor is a smart version of a vector that stores the category labels only once and replaces repeated strings with internal integer codes, saving memory. This efficiency is vital because machine learning algorithms treat nominal and numeric data differently, requiring factors to ensure appropriate handling. For more complex needs, R provides lists, arrays, matrices, and data frames, but they all build upon the foundational vector structure.

Derivation

  1. R data structures are designed for statistical analysis.
  2. Vectors are the fundamental structure, storing ordered, homogeneous data.
  3. Factors are a special vector type for categorical data, storing labels once and using integer codes.
  4. Other structures (lists, arrays, matrices, data frames) handle complex data management tasks.
  5. All these structures interact within an R project, with vectors forming the foundation.

Worked example

Problem

You have a character vector of patient IDs: c("P001", "P002", "P003"). Should you convert this to a factor? Explain why or why not based on the source.

  1. Identify the data type: character vector.
  2. Check the nature of the values: unique identifiers (names or IDs).
  3. Consult the rule for factors: Factors should not be used for character vectors containing mostly unique values like names or IDs.
  4. Conclusion: Do not convert to a factor; keep as a character vector.

Answer No, you should not convert patient IDs to a factor. The source states that factors should not be used for character vectors containing mostly unique values like names or IDs, as they should remain as character vectors.

Common mistakes

  • Assuming all numeric vectors are double type; some functions distinguish between integer and double, so not all numeric vectors are double.
  • Using factors for character vectors that are not truly categorical, such as those with mostly unique values like names or IDs.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.