R vector is simply a list of items of the same type. To combine the list of elements to a vector in R, you can use the “c()” function and separate the items by a comma.
How to Create a Vector in R?
To create a Vector in R, we generally use the “c()” function, but the c() function stands for concatenate. It doesn’t create vectors; it just combines them.
The vector elements must have the same data type, and if they are different, then the function will try and coerce elements to the same type.
Coercion here means from lower to higher types, from logical to an integer to double the character.
Logical vector elements are initialized to FALSE, numeric vector elements to 0, character vector elements to “”, raw vector elements to nul bytes, and list/expression elements to NULL.
# Pro.R rv <- c(11, 21, 19) cat(rv, "\n") cat(typeof(rv))
11 21 19 double
To check the length of a Vector in R, use the length() function.
rv <- c(11, 21, 19) cat(length(rv))
Creating a vector using the (:) operator
We can create a Vector using a colon(:) operator.
rv <- 1:10 cat(rv)
1 2 3 4 5 6 7 8 9 10
Create a vector using the seq() function
We can create a Vector using the seq() function in R. To generate a standard generic sequence in R, use the seq() method.
rv <- seq(1, 5, by = 0.8) cat(rv)
1 1.8 2.6 3.4 4.2 5
Vector of Logical Values
Create a Vector of logical values. In R, TRUE and FALSE are logical values. 1 is not TRUE, and 0 is not FALSE in R.
rv <- c(TRUE, FALSE, FALSE, TRUE, TRUE) cat(rv, "\n") cat(typeof(rv))
TRUE FALSE FALSE TRUE TRUE logical
Create a Vector of Character strings
Let’s create a vector of logical values.
rv <- c("KRU", "NAL", "halloween", "Petrnous") cat(rv, "\n") cat(typeof(rv))
KRU NAL halloween Petrnous character
How to Access Elements of a Vector in R
To access elements in R Vector, use vector indexing. You can access the values of a vector by declaring an index inside a single square bracket “[ ]” operator.
The indexing vector can be categorized in the following.
- Logical vector
- Integer vector
- Character vector
Unlike other programming languages, the square bracket operator returns more than just individual members.
Using a logical vector as an index
If we use a logical vector for indexing, the position where the logical vector is TRUE is returned.
rv <- c(11, 19, 21, 46, 18) cat(rv[c(TRUE, TRUE, TRUE, FALSE, FALSE)])
In this example, the first three indexes are TRUE, TRUE, and TRUE. That means it will return the first three elements in the output; if it encounters FALSE, it won’t return the relevant vector element.
11 19 21
Using integer vector as an index
In R programming, the vector index starts from 1. If you are familiar with other languages, then the index usually starts at 0.
We can use the vector of integers as an index to access particular items. If we use the negative vector of integers, then it will return all elements except those specified.
rv <- c(11, 19, 21, 46, 18) cat(rv)
Let’s access two elements. To access multiple elements, pass the vector of the index.
rv <- c(11, 19, 21, 46, 18) cat(rv[c(2, 3, 4)])
19 21 46
If the index is negative, it will remove the element whose position has the same absolute value as the negative index. Pass the negative vector of integers.
rv <- c(11, 19, 21, 46, 18) cat(rv[c(-2, -3, -4)])
Using character vector as an index
When dealing with named vectors, then using a character vector as an index is all you need.
rv <- c("one" = 21, "two" = 11, "three" = 19) cat(rv["three"])
How to modify a vector in R
To modify a vector in R, use the assignment operator(<-). Modifying the vector is a straightforward task. Let’s see how to do it.
rv <- c(11, 18, 19, 23, 46) cat("Before modifying the vector \n") cat(rv, "\n") rv <- 21 cat("After modifying the vector \n") cat(rv)
Before modifying the vector 11 18 19 23 46 After modifying the vector 11 18 19 21 46
To truncate the element of the vector, use the color operator.
rv <- c(11, 18, 19, 23, 46) cat("Before truncating the vector \n") cat(rv, "\n") rv <- rv[1:3] cat("After truncating the vector \n") cat(rv)
Before truncating the vector 11 18 19 23 46 After truncating the vector 11 18 19
How to delete a Vector in R
To delete a Vector in R, assign NULL to the vector.
rv <- c(11, 18, 19, 23, 46) cat(rv, "\n") cat("After deleting the Vector\n") rv <- NULL print(rv)
11 18 19 23 46 After deleting the Vector NULL
How to sort Vector in R
To sort a vector in R, use the inbuilt sort() method. By default, it sorts in ascending order.
rv <- c(11, 18, 19, 10, 46) res <- sort(rv) cat(res)
10 11 18 19 46
To sort in descending order, we can pass decreasing=TRUE.
rv <- c(11, 18, 19, 10, 46) res <- sort(rv, decreasing = TRUE) cat(res)
46 19 18 11 10