R Vectors


Vectors

A vector is simply a list of objects of the same type.

To compile a vector list, use function c() and comma-separate items.

In the example below, we create a variable vector called fruits, which includes strings:

Example
# Vector of strings
fruits <- c("banana", "apple", "orange")

# Print fruits
fruits

In this example, we create a vector that includes numerical values:


Example
# Vector of numerical values
numbers <- c(1, 2, 3)

# Print numbers
numbers

To create a vector with numerical values ​​respectively, use: operator:


Example
# Vector with numerical values in a sequence
numbers <- 1:10

numbers

You can also create numeric values ​​by decimal, respectively, but be aware that if the last item is not in sequence, it is not used:


Example
# Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1

# Vector with numerical decimals in a sequence where the last element is not used
numbers2 <- 1.5:6.3
numbers2

Result

[1] 1.5 2.5 3.5 4.5 5.5 6.5
[1] 1.5 2.5 3.5 4.5 5.5

In the example below, we create a logical value vector:


Example
# Vector of logical values
log_values <- c(TRUE, FALSE, TRUE, FALSE)

log_values


Vector Length

To find out how many vector objects, use the length() function:


Example
fruits <- c("banana", "apple", "orange")

length(fruits)


Sort a Vector

To sort objects in vector alphabetically or by numbers, use the sort() function:


Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)

sort(fruits)  # Sort a string
sort(numbers) # Sort numbers


Access Vectors

You can access vector objects by referring to its reference number inside brackets []. The first item has point 1, the second item has point 2, and so on:


Example
fruits <- c("banana", "apple", "orange")

# Access the first item (banana)
fruits[1]

You can also access multiple elements by referring to different reference positions with function c():


Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Access the first and third item (banana and orange)
fruits[c(1, 3)]

You can also use negative reference numbers to access all items other than those specified:


Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Access all items except for the first item
fruits[c(-1)]


Change an Item

To change the value of an item, see the reference number:


Example
fruits <- c("banana", "apple", "orange", "mango", "lemon")

# Change "banana" to "pear"
fruits[1] <- "pear"

# Print fruits
fruits


Repeat Vectors

To repeat vectors, use rep() function:


Example

Repeat each value:

repeat_each <- rep(c(1,2,3), each = 3)

repeat_each

Example
repeat_times <- rep(c(1,2,3), times = 3)

repeat_times

Example
repeat_indepent <- rep(c(1,2,3), times = c(5,2,1))

repeat_indepent


Generating Sequenced Vectors

In one of the examples above, we have shown you how to create a vector with numerical values ​​in sequence: operator:


Example
numbers <- 1:10

numbers

To perform larger or smaller steps in sequence, use the seq function ():


Example
numbers <- seq(from = 0, to = 100, by = 20)

numbers

Note: The function of seq() has three parameters: from where the sequence begins, to where the sequence stops, and by it is the sequence period.