i

R Programming Complete Tutorial

Working with Vector

Data Handling
In this part of the document, we will discuss detail on Vector, List, Matrix, Array, Data Frame, Factors, and Contingency tables. We will start with the vector.


Working with Vector

A vector is a sequence of data elements that share the same data type. In fact, a vector is a simple data structure in R. We will use it regularly, often as a building block on those systems with more complex data structures and operations.

Assigning a vector

A vector can be assigned in three ways. First, using the c() function. Since a vector contains elements of the same type, this function will try and coerce elements to the same type, if they are different.

In the first example, we are assigning a numeric vector of three elements. The next one is a vector of logical values, and the final one is a character vector.

Using c() function:

We can assign a vector using the colon operator. It is easy to use, we have to just use a colon in between two numbers, and it will create a sequence of numeric vectors.We are assigning a numeric vector with the values from 5 to 10.

Using the Operator:

Using seq():

Another way of creating a vector is with the seq() function.

In this example, we are assigning a vector with 2 to 7 numeric values.