2.1 Introduction

Mastering R (or any other programming language) essentially consists in solving two inter-related tasks:

  1. Representing various types of information as objects (Section 2.2.1).

  2. Manipulating those objects by using functions (Section 2.4).

This chapter introduces both of these tasks. Later, we will see that analyzing data often involves creating and manipulating larger data structures (Chapter 3) and that programming also involves creating our own functions (Chapter 12). But before we can address those issues, we need to grasp the elementary distinction between between “data” (as some object that represents particular types of information) vs. “functions” (as operators that process and transform information).

The main type of object used in R for representing information is a vector. Vectors come in different types and have some special properties that we need to know in order to use them in a productive fashion. As we will see, manipulating vectors by exploiting their properties and applying functions to them allows for quite a bit of data analysis — and all the rest are details and extensions…

2.1.1 Contents

This chapter introduces basic concepts and essential R commands. It rests on the distinction between data objects and functions and introduces vectors as the main data structure in R. Vectors come in different data types (e.g., character, numeric, logical) and are manipulated by matching functions. Key concepts for working with vectors are “assignment,” “recycling,” and “filtering” (indexing or subsetting).

Note that this chapter — in contrast to Chapter 3) — does not deal with data structures beyond vectors.

2.1.2 Data and tools

This chapter only uses base R functions and snippets of example data that we will generate along the way. Some examples are using functions from the ds4psy package.