Creating a vector

A vector is a basic data structure that represents a one-dimensional array. to create a array we use the “c” function which the most common method use in R Programming Language.

R




# R program to create Vectors
 
# we can use the c function
# to combine the values as a vector.
# By default the type will be double
X<- 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:7
cat('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

R Vectors

R Vectors are the same as the arrays in R language which are used to hold multiple data values of the same type. One major key point is that in R Programming Language the indexing of the vector will start from ‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well. 

R – Vector

Similar Reads

Creating a vector

A vector is a basic data structure that represents a one-dimensional array. to create a array we use the “c” function which the most common method use in R Programming Language....

Types of R vectors

...

Length of R vector

Vectors are of different types which are used in R. Following are some of the types of vectors:...

Accessing R vector elements

...

Modifying a R vector

...

Deleting a R vector

...

Sorting elements of a R Vector

In R, the length of a vector is determined by the number of elements it contains. we can use the length() function to retrieve the length of a vector....