R Vectors - R Programming language

What are R Vectors?

Vectors are known as the R data objects. There are six types of atomic vectors available like logical, integer, double, complex, character and raw.

Vector Creation

Single Element Vector

Even when you write just one value in R, it becomes a vector of length 1 and belongs to one of the above vector types.
When we execute the above code, it produces the following result –

Multiple Elements Vector

Using colon operator with numeric data
When we execute the above code, it produces the following result –
Using sequence (Seq.) operator
When we execute the above code, it produces the following result –
Using the c() function
The non-character values are coerced to character type if one of the elements is a character.
When we execute the above code, it produces the following result –

Accessing Vector Elements

Elements of a Vector are accessed using indexing. The [ ] brackets are used for indexing. Indexing starts with position 1. Giving a negative value in the index drops that element from result.TRUE, FALSE or 0 and 1 can also be used for indexing.
When we execute the above code, it produces the following result –

Vector Manipulation

Vector arithmetic

Two vectors of same length can be added, subtracted, multiplied or divided giving the result as a vector output.
When we execute the above code, it produces the following result –

Vector element recycling

If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations.
When we execute the above code, it produces the following result –

Vector Element Sorting

Elements in a vector can be sorted using the sort() function.
When we execute the above code, it produces the following result –

R Programming language Topics