Vectors

A vector is a list of items that are of the same data type.

To combine a list of items into a vector, use c() function and separate the items by a comma.

 

There are two broad categories of vectors:

Atomic Vectors:

logical, integer, double, character, complex, and raw. Double and integer vectors are collectively called numeric vectors.

List Vectors:

List vectors are also called recursive vectors because lists can contain other lists. A list can be heterogenous.

The hierarchy of R's vector types

Every vector has 2 key properties:

  1. type: to determine type, use the typeof() function. For example,
  2. length: to determine length, use the length() function.
Vector Type Explanation Examples
Numeric These vectors store numeric values, which can be either integers or real numbers (floating-point). Numeric vectors are the most commonly used in R. numeric_vector <- c(1, 2, 3, 4.5, -0.5)
Integer Integer vectors are specifically designed to hold whole numbers. They are indicated by adding the “L” suffix to the numbers. integer_vector <- c(1L, 2L, 3L, -5L)
Character Character vectors store text or string data. Each element of a character vector is a character string enclosed in quotes. character_vector <- c(“apple”, “banana”, “cherry”)
Logical Logical vectors contain Boolean values, which can be either TRUE or FALSE or NA. They are often used for conditional expressions and logical operations. logical_vector <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
Complex Complex vectors hold complex numbers with real and imaginary components. Complex numbers are used in mathematical and engineering applications. complex_vector <- c(1 + 2i, 3 – 4i, 5 + 6i)
Raw Raw vectors are used to store binary data in its raw form. They are often employed for reading and writing binary files.
Date and Time R provides specialized data types for dates and times, including Date, POSIXct, and POSIXlt, which allow for handling date and time data. date_vector <- as.Date(“2023-10-18”)
datetime_vector <- as.POSIXct(“2023-10-18 15:30:00”)
Factor Factor vectors are used to represent categorical data with predefined levels. They are commonly used in statistical analyses. factor_vector <- factor(c(“Low”, “Medium”, “High”), levels = c(“Low”, “Medium”, “High”))

Length of a Vector

To find out the length of a vector, use the length() function. This function tells us how many items are in a vector.

For example,

my_friends <- c("Tom", "Jerry", "Paulina", "Preston")

#find out the length of the variable

length(my_friends)

Sorting Vectors

sorting syntax: sort(x, decreasing = FALSE, ...)

Sorting by Ascending Order

funny_numbers <- c(2,3,5,7,9,5,6,7,8,9,11,13,24,56)
sort(funny_numbers, decreasing = TRUE)

Accessing Vectors

You can access the vector items by referring to its index number inside brackets []. The first item has index 1, the second item has index 2, and so on. To access multiple index positions within a vector, we can use the index position position followed by the comma for other positions inside of the vector using the c() function. For exmaple,

my_friends[2]

my_friends[c(2, 3,4)]

Excluding Vectors

specific items from a vector may also be excluded using their index position preceded by the minus (-) smbol. For example, to return a list of my friends and ignore “Jerry”, that could be done like this:

my_friends [c(-2)]

Change Vectors

The vector that is located in vector may be changed by utilizing its index position. This is done by assinging a value to that index position. For example, I could add a friend to the 5th index position. In the 5th index position I am going to add another friend, Marcus.

my_friends[5] <- “Marcus”

 

Now, if you run a print of the friends, you will see that Marcus was addeded. This same principle can be applied to change

 

Repeat Vectors

Vectors may be repeated using the rep() function. The vector may be repeated a number of times or

Repeat by number of times

For the repeat examples, we will make up a new vector, my_xyz. Let us repeat “x”, “y” and “z” 3 times.

Below, the times sections tells the program to repreat the sequence 3 times using the times  parameter.

rep(vector, times = 2)

my_xyz <- rep(c(“x”,”y”,”z”), times=3)

Print(my_xyz)

my_xyz [1] “x” “y” “z” “x” “y” “z” “x” “y” “z”

using each parameter:

Sequencing a vector

To generate a sequence of numbers in R we use the : operator

count_by1 <- 1:10

The above can also be written as

seq(from = 1, to = 10, by = 1)

Example 2: let us count from 10-99 and skip 3

by_three <- seq(from =10, to = 99, by = 3)

print(by_three)

by_three [1] 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97

 

Lists

Lists are different from other atomic vectos because their elements can be of any type-  like dates, data frames, vectors, matrices and more. Lists can also contain other lists.

Creating lists

other lists can be created using list() function.

For example:

my_list <- list("cat", 2L, 1.5, 7)

print(my_list)

Leave a Comment

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

Scroll to Top