Chapter 3 – Objects, Vectors, and Functions

Christine Monnier

Now that we are familiar with our interface, it is time to get started with the actual R language. As we know from the previous chapter, R is a sophisticated calculator, specifically appropriate for statistics and data science.

I highly recommend following along with the videos below in your own R Studio Cloud Workspace.

Learning Objectives

In this chapter, we will cover the following topics:

  • objects;
  • vectors;
  • functions.

With objects, vectors and functions, we are really getting into the R language so this is where things get serious.

For those of you taking a class, you will be provided an empty script with only the instructions. You should type the tutorial code as you follow along. Writing code yourself, as opposed to just watching the videos or just running code provided by someone else, is the best way to learn and get more comfortable.

Part 1 – Introducing objects, vectors, and functions

The first video in this chapter walks you through the basics of objects, vectors, and functions.

Objects are the basic building blocks of the R language. They are what we create, manipulate, model, visualize in order to extract information from them. Vectors are a type of object in R. They are one-dimensional series of numbers, or characters, or logical expressions. In the video below, we’ll create our first objects and vectors and conduct some basic manipulation using functions.

Functions are another key building block of the R language. Think of functions as verbs that we apply to objects. Functions do something to objects. This first video also introduces you to some basic functions.

Part 2 – Using functions to automatically create objects

In this section, we will expand our understanding of objects, vectors, and functions by generating vectors using three functions that automate that process based on parameters we set. It’s less complicated and abstract than it sounds.

Part 3 – Logical and character vectors

So far in this chapter, we have examined numeric vectors. In this last section, we will examine the other two types of vectors we will encounter: logical and character vectors.

We will also introduce the concepts of vector recycling and coercion.

This concludes your first steps with the R language. Hopefully, you are starting to understand how it works: we create objects, such as vectors, and then, we manipulate or extract information from these objects, using the appropriate functions. This is the logic we will apply throughout the semester, albeit with more complex objects and functions, but the underlying reasoning is the same.

Key functions used in this chapter

Part 1

  • c(): the function that creates basic objects and vectors;
  • sum(): the function that calculates the sum of all the elements in a numeric object;
  • min(): the functions that identifies the smallest value in a numeric object;
  • max(): the function that identifies the largest value in a numeric object;
  • mean(): the function that calculates the mean of a numeric object;
  • median(): the function that calculates the median of a numeric object;
  • summary(): the function that provides summary statistics for a numeric object;
  • length(): the function that calculates the length (number of elements) of a numeric object;
  • help(): the function that opens the help documentation on any function.

Part 2

  • seq(): the function that generates a sequence of numbers;
  • rep(): the function that generate a repeating series of numbers;
  • sample(): the function that randomly generates a series of numbers;
  • sort(): the function that sorts a series of number in increasing or decreasing order.

Part 3

  • paste(): the function that joins character vectors.

Check your understanding by taking the quiz below.

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

R and R Studio For Absolute Beginners Copyright © 2022 by Christine Monnier is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book