R – Vector

22 Apr, 2020
R programming is one of the most popular languages when it comes to data science, statistical computations or scientific research. R programming is widely used in machine learning and it is very efficient and user-friendly. It provides flexibility in doing big statistical operations with a few lines of code.
Vectors in R are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from ‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well.
Vectors are of different types which are used in R. Following are some of the types of vectors:
Numeric vectorsNumeric vectors are those which contain numeric values such as integer, float, etc.# R program to create numeric Vectors  # creation of vectors using c() function.v1 <- c(4, 5, 6, 7)  # display type of vectortypeof(v1)  # by using 'L' we can specify that we want integer values.v2 <- c(1L, 4L, 2L, 5L)   # display type of vectortypeof(v2)Output:[1] "double"
[1] "integer"
# R program to create numeric Vectors  # creation of vectors using c() function.v1 <- c(4, 5, 6, 7)  # display type of vectortypeof(v1)  # by using 'L' we can specify that we want integer values.v2 <- c(1L, 4L, 2L, 5L)   # display type of vectortypeof(v2)
Output:
[1] "double"
[1] "integer"
Character vectorsCharacter vectors contain alphanumeric values and special characters.# R program to create Character Vectors  # by default numeric values # are converted into charactersv1 <- c('geeks', '2', 'hello', 57)   # Displaying type of vectortypeof(v1)Output:[1] "character"
# R program to create Character Vectors  # by default numeric values # are converted into charactersv1 <- c('geeks', '2', 'hello', 57)   # Displaying type of vectortypeof(v1)
Output:
[1] "character"
Logical vectorsLogical vectors contain boolean values such as TRUE, FALSE and NA for Null values.# R program to create Logical Vectors  # Creating logical vector# using c() functionv1 <- c(TRUE, FALSE, TRUE, NA)  # Displaying type of vectortypeof(v1)Output:[1] "logical"
# R program to create Logical Vectors  # Creating logical vector# using c() functionv1 <- c(TRUE, FALSE, TRUE, NA)  # Displaying type of vectortypeof(v1)
Output:
[1] "logical"
There are different ways of creating vectors. Generally, we use ‘c’ to combine different elements together.
# R program to create Vectors  # we can use the c function# to combine the values as a vector.# By default the type will be doubleX <- c(61, 4, 21, 67, 89, 2)cat('using c function', X, '\n')  # seq() function for creating# a sequence of continuous values.# length.out defines the length of vector.Y <- seq(1, 10, length.out = 5) cat('using seq() function', Y, '\n')   # use':' to create a vector # of continuous values.Z <- 2:7cat('using colon', Z)
Output:
using c function 61 4 21 67 89 2 
using seq() function 1 3.25 5.5 7.75 10 
using colon 2 3 4 5 6 7

Accessing elements in a vector is the process of performing operation on an individual element of a vector. There are many ways through which we can access the elements of the vector. The most common is using the ‘[]’, symbol.
Note: Vectors in R are 1 based indexing unlike the normal C, python, etc format.
# R program to access elements of a Vector  # accessing elements with an index number.X <- c(2, 5, 18, 1, 12)cat('Using Subscript operator', X[2], '\n')  # by passing a range of values# inside the vector index.Y <- c(4, 8, 2, 1, 17)cat('Using combine() function', Y[c(4, 1)], '\n')  # using logical expressionsZ <- c(5, 2, 1, 4, 4, 3)cat('Using Logical indexing', Z[Z>4])
Output
Using Subscript operator 5 
Using combine() function 1 4 
Using Logical indexing 5

Modification of a Vector is the process of applying some operation on an individual element of a vector to change its value in the vector. There are different ways through which we can modify a vector:
# R program to modify elements of a Vector  # Creating a vectorX <- c(2, 7, 9, 7, 8, 2)  # modify a specific elementX[3] <- 1X[2] <-9cat('subscript operator', X, '\n')  # Modify using different logics.X[X>5] <- 0cat('Logical indexing', X, '\n')  # Modify by specifying # the position or elements.X <- X[c(3, 2, 1)]cat('combine() function', X)
Output
subscript operator 2 9 1 7 8 2 
Logical indexing 2 0 1 0 0 2 
combine() function 1 0 2

Deletion of a Vector is the process of deleting all of the elements of the vector. This can be done by assigning it to a NULL value.
# R program to delete a Vector  # Creating a VectorM <- c(8, 10, 2, 5)  # set NULL to the vectorM <- NULL cat('Output vector', M)
Output:
Output vector NULL

sort() function is used with the help of which we can sort the values in ascending or descending order.
# R program to sort elements of a Vector  # Creation of VectorX <- c(8, 2, 7, 1, 11, 2)  # Sort in ascending orderA <- sort(X)cat('ascending order', A, '\n')  # sort in descending order # by setting decreasing as TRUEB <- sort(X, decreasing = TRUE)cat('descending order', B)
Output:
ascending order 1  2  2  7  8 11
descending order 11  8  7  2  2  1

Picked
R Data-types
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.