Vectors

When working with datasets, a variable does not contain just a single value, but rather the values of that variable for all respondents. For example, if we have a variable “income,” it lists all income values of all respondents. In this case, we also refer to it as a vector: the individual values of the respondents are the components of the vector.

Let’s take an example of five individuals with income values: \(1432\), \(982\), \(5139\), \(3210\), and \(726\).

Represented as a vector, it would look like this:

\[ income = \left(\begin{array}{c} 1432 \\ 982 \\ 5139 \\ 3210 \\ 726 \end{array}\right) \] A vector is essentially a chain of values.

Vectors in R

A vector can contain any number of numbers, letters, or a combination of both. It can consist of one of the four value types you have previously learned.

Just as a reminder, here are the value types:

  • numeric: \(1.456783135786\), \(2.5467\), \(-3.45786\)

  • integer: \(1\), \(27\), \(1453\), \(-144\), \(545\)

  • character: any letter/character or a combination of characters and numbers

  • logical: TRUE or FALSE

In R, we combine different values into an object using a function, turning the new object into a vector. A function is a pre-programmed instruction that can be called and applied using a specific command. R provides comprehensive libraries (we will learn about this next time!) with thousands of functions. But don’t worry, no one knows all of them by heart. The most important skill in the R course is to understand programming, as this allows you to learn new functions or reacquaint yourself with forgotten functions.

The function we want to use now is called with c(). Inside the parentheses, we list all the values we want to combine into a vector in an object. What is inside the parentheses of a function is also called an argument. We separate the values/arguments with a comma.

Let’s make an example where we combine multiple numerical values:

math <- c(
  0.2, 
  0.4, 
  0.6, 
  0.8, 
  1.0
)

math
## [1] 0.2 0.4 0.6 0.8 1.0

The output will no longer display just one value, but all the values stored in the vector. A vector can be of any length.

In the Environment, we can now see that in addition to the value, the type of the vector is specified (here num). After the type, the length of the vector is indicated: [1:5]. We start counting the vector at 1 and it ends at 5. Important: In many other languages, counting starts at 0.

Vector in the Environment
Vector in the Environment

We can also create vectors of arbitrary length with the other data types:

logi <- c(
  TRUE, 
  FALSE
)

numbers <- c(
  1,
  2, 
  3, 
  4, 
  5, 
  6, 
  7, 
  8, 
  9, 
  10
)

char <- c(
  "Taipeh", 
  "Seoul",
  "Berlin",
  "Taipeh"
)

In the Environment, we will find all these vectors again along with their respective types:

Vectors in the Environment
Vectors in the Environment

What happens if we try to mix multiple data types in a vector? Run the code and check in the Environment to see which type is assigned.

mix <- c(
  1.231, 
  "Berlin",
  TRUE
)

Another Function

Now we will learn about another function, or rather four more functions, that we can use to test the type of an object. These functions are:

  1. is.numeric(),

  2. is.integer(),

  3. is.character(), and

  4. is.logical()

In the parentheses, we input the object we want to test, which we have stored in the Environment. So, there is only one argument in this function. The output will be a logical value: TRUE or FALSE, depending on whether it is true or not.

Try it with the stored objects from above. When does the property apply? Simply copy the code into your script and then execute the functions/commands!

is.numeric(numbers)

is.integer(math)

is.logical(logi)

is.character(logi)

Individual Elements within a Vector

Sometimes we want to know what the x-th value of a vector is. This can be solved by using conditions on an object. Conditions on an object are specified in square brackets []. For vectors, we only have one dimension, which means we can only specify one argument here.

For example, we want to know which city Person \(3\) has specified in the variable char. We use the object and specify (without spaces) the desired position in square brackets (here, \(3\)):

char[3]
## [1] "Berlin"

So, Person \(3\) has specified the city of Berlin.

This small application will be important later when recoding and editing variables, so feel free to take a second look or try it out with other objects and different positions.

Besides vectors, what other types of objects are there in R?