The R vector is a list of items that are of the same type. The c() function is used to combine the list of items to a vector after separating the items by a comma.
Below example shows a vector variable language, that combines strings:
language <- c("Python", "R", "Java") language
Output
In this example, we create a vector that combines numerical values:
num <- c(1, 2, 3) num
Output
Use the (:) operator, to create a vector with numerical values in a sequence:
num <- 1:10 num
Output
We can create numerical values with decimals in a sequence also, but note that it is not used if the last element does not belong to the sequence:
# Vector with numerical decimals in a sequence num1 <- 1.5:6.5 num1 # Vector with numerical decimals in a sequence where the last element is not used num2 <- 1.5:6.3 num2
Output
In this example, a vector of logical values is created:
l <- c(TRUE, FALSE, TRUE, FALSE) l
Output
R Vector Length
The length() function is used to find how many items a vector has.
language <- c("Python", "R", "Java") length(language)
Output
Sort R Vector
The sort() function is used to align the items in a vector either alphabetically or numerically:
language <- c("Python", "R", "Java", "C++", "Ruby", "HTML") n <- c(23, 83, 55, 17, 210, 82) sort(language) sort(n)
Output
Access R Vectors
Vector items can be accessed by referring its index number inside brackets []. The first item has index 1 and second item has index 2 and so on.
language <- c("R","Python", "Java") # Access the 2nd item (Python) language[2]
Output
We can also access multiple elements by referring to different index positions with the c() function:
language <- c("Python", "R", "Java", "C++", "Ruby", "HTML") # Access the first and third item (Python and Java) language[c(1, 3)]
Output
To access all items except the ones specified, we can also use negative index numbers:
language <- c("Python", "R", "Java", "C++", "Ruby", "HTML") # Access all items except Python language[c(-1)]
Output
Change an Item
Refer to the index number, to change the value of a specific item:
language <- c("Python", "R", "Java", "C++", "Ruby", "HTML") # Change "Python" to "CSS" language[1] <- "CSS" language
Output
Repeat Vectors
To repeat vectors we used the rep() function:
r <- rep(c(11,12,13), each = 2) r
Output
The above example repeats each value.
r <- rep(c(11,12,13), times = 5) r
Output
The above example repeat the sequence of the vector
r <- rep(c(11,12,13), times = c(3,1,2)) r
Output
The above example repeats each value independently.
Generating Sequenced R Vector
We can create a vector with numerical values in a sequence with the ( : ) operator:
num <- 1:10 num
Output
Use the seq() function to make bigger or smaller steps in a sequence:
num <- seq(from = 0, to = 50, by = 10) num
Output
The seq() function has three parameters:
- from (where the sequence starts)
- to (where the sequence stops)
- by (interval of the sequence)
R Vector Operation
There are different ways to perform operations on the vector. Mathematical operations like add, subtract, division, or multiplication of two or more vectors from each other can be done. R programming plays important role in data science. For data manipulation, these operations are required. Have a look at the following type of operations that are performed on the vector.
Combining R vectors
The c() function is used for two purposes create a vector and concatenate two vectors. After combining one or more vectors, it forms a new vector that contains all the items of each vector. Have a look at the following example, how the c() function concatenate the vectors:
p<-c(1,2,4,5,7,8) q<-c("Python", "R", "Java", "C++", "Ruby", "HTML") r<-c(p,q) r
Output
Arithmetic operations
Arithmetic operations can be performed on vectors. The arithmetic operations are performed member by a member on vectors. You can add, subtract, multiply or divide two vectors. Have a look at how arithmetic operations are performed vectors
a<-c(23,6,54,5) b<-c(2,67,8,3) a+b a-b a/b a%%b
Output
Logical Index vector
You can form a new vector from a given vector with help of the logical index vector in R Programming. The length will be the same as the original vector. At only one stage the vector members are true when the answering members of the logical vector are included in the slice, otherwise, it comes false. Have a look at the following example of how a new vector is created with help of a logical index vector:
a<-c("Python", "R", "Java", "C++", "Ruby", "HTML") b<-c(TRUE,FALSE,TRUE,TRUE,FALSE,FALSE) a[b]
Output
Numeric Index
In R programming we set the index between square [ ] braces for indexing a numerical value. In case the index value is negative it will return all the values excluding for the index we have set. For example, specifying [-5] will prompt R to convert -5 into its absolute value and then search for the value which occupied that index.
q<-c("Python", "R", "Java", "C++", "Ruby", "HTML") q[2] q[-4] q[15]
Output
Duplicate Index
The index vector permit duplicate values means you can access one element twice in a single operation. Have a look at the below example, how duplicate indexes work.
q<-c("Python", "R", "Java", "C++", "Ruby", "HTML") q[c(2,4,4,3)]
Output
Range Indexes
To form a new vector, a range index is used to slice the vector. The colon ( : ) operator is used for slicing. Range indexes are most helpful for the situation involving a large operator. Have a look at an example of how slicing is done with the help of the colon operator to form a new vector.
q<-c("Python", "R", "Java", "C++", "Ruby", "HTML") b<-q[2:5] b
Output
Out-of-order Indexes
The index vector can be out of order in R programming. Following is the example of a vector slice with the order of first and second values reserved.
q<-c("Python", "R", "Java", "C++", "Ruby", "HTML") b<-q[2:5] q[c(2,1,3,4,5,6)]
Output
Named vectors members
First create the vector of characters as:
Once the vector of characters is created, name the 1st vector member as “Start” and the 2nd member as “End”:
We can retrieve the first member by its name as follows:
We can reverse the order with the help of the character string index vector.
The complete example for naming vector members is given below:
a=c("TensorFlow","PyTorch") a names(a)=c("Start","End") a a["Start"] a[c("Second","First")]
Output