R Vector

In this article, you will learn about vectors in R programming with the help of examples.

Vector is a basic data structure in R. It contains elements of the same type. The data types can be logical, integer, double, character, complex or raw.

A vector's type can be checked with the typeof() function.

Another important property of a vector is its length. This is the number of elements in the vector and can be checked with the function length().


How to Create Vector in R

Vectors are generally created using the c() function.

Vector in R Programming
Vector in R Programming

Since, a vector must have elements of the same type, this function will try and coerce elements to the same type, if they are different.

Coercion is from lower to higher types from logical to integer to double to character.

Let's create a vector in R:

x <- c(1, 5, 4, 9, 0)
typeof(x)

length(x)
x <- c(1, 5.4, TRUE, "hello")
x
typeof(x)

Output

[1] "double"
[1] 5
[1] "1"     "5.4"   "TRUE"  "hello"
[1] "character"

In the above example, we have created a numeric vector x using the c() function. The vector contains elements 1, 5, 4, 9, and 0.

The typeof() function is then used to determine the data type of the vector x, which in this case is "double" since all the elements are numeric.


Creating a vector using : operator

# create vector x
x <- 1:7
x
# create vector y
y <- 2:-2
y

Output

[1] 1 2 3 4 5 6 7
[1]  2  1  0 -1 -2

In the above example, we have created two vectors, x and y using the : operator. The operator : is used to generate a sequence from 1 to 7. The resulting vector is assigned to the variable x.

Again, the operator : is used to generate a sequence from 2 to -2. The resulting vector is assigned to the variable y.


Creating a vector using seq() function

The seq() function in R allows you to generate sequences with specific step sizes or lengths.

seq(1, 3, by=0.2) # specify step size

seq(1, 5, length.out=4) # specify length of the vector

Output

[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
[1] 1.000000 2.333333 3.666667 5.000000

In the first line, the seq() function is used to create a sequence from 1 to 3 with a step size of 0.2.

This means that the sequence will start at 1 and increment by 0.2 until it reaches or slightly surpasses 3. The resulting sequence is returned by the seq() function.

In the second line, the seq() function is used to create a sequence from 1 to 5 with a desired length of 4.


How to access Elements of a Vector?

Elements of a vector can be accessed using vector indexing. The vector used for indexing can be logical, integer or character vector.

Using integer vector as index

Vector index in R starts from 1, unlike most programming languages where the index starts from 0.

We can use a vector of integers as index to access specific elements.

We can also use negative integers to return all elements except those specified.

But we cannot mix positive and negative integers while indexing and real numbers, if used, are truncated to integers.

x <- 1:7
# access 3rd element
x[3]    
# access 2nd and 4th element
x[c(2, 4)]     
# access all but 1st element
x[-1]          
# cannot mix positive and negative integers
x[c(2, -4)]

Output

[1] 3
[1] 2 4
[1] 2 3 4 5 6 7
ERROR!
Error in x[c(2, -4)] : only 0's may be mixed with negative subscripts
Execution halted

Using logical vector as index

When we use a logical vector for indexing, the position where the logical vector is TRUE is returned.

This useful feature helps us in filtering vectors as shown below.

x <- 1:5
x[c(TRUE, FALSE, FALSE, TRUE)]
# filtering vectors based on conditions
x[x < 0] 
x[x > 0]

Output

[1] 1 4 5
integer(0)
[1] 1 2 3 4 5

In the above example, the expression x>0 will yield a logical vector (FALSE, FALSE, FALSE, TRUE) which is then used for indexing.

Using character vector as index

This type of indexing is useful when dealing with named vectors. We can name each elements of a vector.

x <- c("first"=3, "second"=0, "third"=9)
names(x)
x["second"]

Output

[1] "first"  "second" "third" 
second 
     0

How to modify a vector in R?

We can modify a vector using the assignment operator.

We can use the techniques discussed above to access specific elements and modify them.

If we want to truncate the elements, we can use reassignments.

x <- c(-3,-2,-1,0,1,2)
# modify 2nd element
x[2] <- 0; x        
# modify elements less than 0
x[x<0] <- 5; x   
# truncate x to first 4 elements
x <- x[1:4]; x      

Output

[1] -3  0 -1  0  1  2
[1] 5 0 5 0 1 2
[1] 5 0 5 0

How to delete a vector in R?

We can delete a vector by simply assigning a NULL to it.

x <- c(-3,-2,-1,0,1,2)
x <- NULL
x
x[4]

Output

NULL
NULL
Did you find this article helpful?