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.

Every vector has 2 key properties:

- type: to determine type, use the typeof() function. For example,
- 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)

1 2 3 4 5 6 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDC4B ace_keyword">> </span><span class="GNVWDDMDB3B ace_keyword">sort(funny_numbers, decreasing = TRUE) </span><span class="GNVWDDMDL3B"> [1] 56 24 13 11 9 9 8 7 7 6 5 5 3 2 <strong>Sorting in Descending Order</strong> sort(funny_numbers, decreasing = FALSE) </span></span> |

1 2 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDC4B ace_keyword">> </span><span class="GNVWDDMDB3B ace_keyword">sort(funny_numbers, decreasing = TRUE) </span><span class="GNVWDDMDL3B"> [1] 56 24 13 11 9 9 8 7 7 6 5 5 3 2</span></span> |

1 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDL3B"> </span></span> |

1 |

## 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:

1 2 3 4 5 |
<span class="hljs-selector-tag">rep</span>(vector, each = <span class="hljs-number">2</span>) my_xyz <- rep(c("x", "y", "z"), each = 3) print(my_xyz) |

1 2 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDC4B ace_keyword">> </span><span class="GNVWDDMDB3B ace_keyword">my_xyz </span><span class="GNVWDDMDL3B">[1] "x" "x" "x" "y" "y" "y" "z" "z" "z"</span></span> |

1 |

## 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)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDC4B ace_keyword">> </span><span class="GNVWDDMDB3B ace_keyword">print(my_list) </span><span class="GNVWDDMDL3B">[[1]] [1] "cat" [[2]] [1] 2 [[3]] [1] 1.5 [[4]] [1] 7 From the above it can be seen that the print function prints out each index position and the value that is in that position. String Structure The structure function, str(), is a useful tool when working with lists. This function tells us about the structure of the list and not its contents. str(my_list) </span></span> |

1 2 3 4 5 6 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDC4B ace_keyword">> </span><span class="GNVWDDMDB3B ace_keyword">str(my_list) </span><span class="GNVWDDMDL3B">List of 4 $ : chr "cat" $ : int 2 $ : num 1.5 $ : num 7</span></span> |

1 2 3 4 5 6 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDL3B">Naming Lists Lists can be name. You can name the elements of a list when you first create it with the list() function list("apples = 2", "oranges = 4", "mangoes = 8") </span></span> |

1 2 3 4 5 6 7 8 9 10 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDC4B ace_keyword">> </span><span class="GNVWDDMDB3B ace_keyword">list("apples = 2", "oranges = 4", "mangoes = 8") </span><span class="GNVWDDMDL3B">[[1]] [1] "apples = 2" [[2]] [1] "oranges = 4" [[3]] [1] "mangoes = 8" </span></span> |

1 |
<span class="GNVWDDMDM3B" tabindex="-1" role="document"><span class="GNVWDDMDL3B"> </span></span> |