R Data Structure: Vector
Aug 25, 2019 | Updated: Aug 25, 2019
# R

Data structure is a very basic and essential concept in a programming language like R. This article will explain the details of vector data structure in R.


What is Vector in R?

Vector is a basic data structure in R which contains element of the same data type.

The type of a vector object can be checked using the typeof() built-in function.

The length() is used to check the length of a vector object.

Also, you can use is.vector() function to check whether a object is vector or not.


How to Create Vector in R

Usually, in R, vectors are created using c() function.

my_vector <- c(10, 20, 30)
print(my_vector)
[1] 10 20 30

# Check length of the vector object
length(my_vector)
[1] 5

# Return TRUE as it is a vector
is.vector(my_vector)      
[1] TRUE

If a vector is tried to create with different types of elements then the elements are coerced into the same type (from lower to higher). That means, elements are coerced from logical to integer to double to character.

# Element types: logical, integer, double, and character
my_vector_1 <- c(TRUE, 2L, 3, "R")
typeof(my_vector_1)
[1] "character"

# Element types: logical, integer, and double
my_vector_2 <- c(TRUE, 2L, 3)
typeof(my_vector_2)
[1] "double"

# Element types: logical, and integer
> my_vector_3 <- c(TRUE, 2L)
> typeof(my_vector_3)
[1] "integer"

You can create a vector of sequential or consecutive numbers by using : operator. In that case you can create the vector with or without c() function. That's amazing!

# Create a vector of 1 to 9 with c()
my_vector_1 <- c(1:9)
print(my_vector_1)
[1] 1 2 3 4 5 6 7 8 9

# Create a vector of 1 to 9 without c()
my_vector_2 <- 1:9
print(my_vector_2)
[1] 1 2 3 4 5 6 7 8 9

A vector of more complex sequence can be created using the seq() function, in which you can specify the step size from one element to the next element.

Alternatively, you can create the vector of complex sequence with a desired length of the vector.

Wow! R is superb! Isn't it?

# A vector of sequence from 2 to 5 with step size 0.5
my_vector_1 <- seq(2, 5, by=0.5, length.out = 3)
print(my_vector_1)
[1] 2.0 2.5 3.0 3.5 4.0 4.5 5.0

# A vector of sequence from 2 to 5 with a desired length 3
my_vector_2 <- seq(2, 5, length.out = 3)
print(my_vector_2)
[1] 2.0 3.5 5.0
length(my_vector_2)
[1] 3 


How to Access Elements of a Vector?

Elements of a vector can be accessed using integer, logical, or character vector index. The index is placed inside the [] brackets.

Integer vector Index:

In R, vector index starts from 1, which is different from most programming languages where index starts from 0. Positive index value returns the corresponding element whereas negative value drops the corresponding element.

Note that, positive and negative indexes cannot be mixed together, but only 0's may be mixed with negative index. Also, real number as index truncated to integer.

# Create a vector
my_vector <- c(10, 20, 30, 40, 50)

# Return element at position 2
print(my_vector[2])
[1] 20

# Return elements at position 2 and 4
print(my_vector[c(2,4)])
[1] 20 40

# Drop element at position 3, and return rest of the elements
print(my_vector[c(-3)])
[1] 10 20 40 50

# Gives Error: Cannot mix positive and negative indexes  
print(my_vector[c(-3, 5)])
Error in my_vector[c(-3, 5)] : "only 0's may be mixed with negative subscripts"

# Real numbers are truncated to integer indexes
# So, return elements at position 3 and 5
print(my_vector[c(3.8, 5.2)])
[1] 30 50

Logical Vector Index:

When a logical index is TRUE, then according to the position of the index the corresponding element of the vector object is return, whereas FALSE index drops the element. Logical vector index is useful in conditional filtering of vector objects.

# Create a vector
my_vector <- c(10, 20, 30, 40, 50)

# Return element at position 1, 3, and 4
print(my_vector[c(TRUE, FALSE, TRUE, TRUE, FALSE)])
[1] 10 30 40

# Return elements > 30
# That means, print(my_vector[c(FALSE, FALSE, FALSE, TRUE, TRUE)])
print(my_vector[my_vector > 30])
[1] 40 50

Character Vector Index:

Character vector as an index is useful for the named vectors.

# Create a vector
my_vector <- c("year"=2010, "month"=10, "day"=20)

# Print the vector object
print(my_vector)
 year month   day 
 2010    10    20

# Print element of "year" index only
print(my_vector["year"])
year 
2010

# Print elements of "month" and "day" indexes
print(my_vector[c("month", "day")])
month   day 
   10    20 


Final Talks

Great! You have just explored vector, one of the data structures in R. Let's move forward to other data structures in R, such as list, factor, and data frame.

Happy R coding!


Comments

You are welcome to write comments, suggestions, corrections, or any queries related to the article. Your comments may take some time to be appeared. Please be aware that any irrelevant comments will be deleted. Thanks for your understanding, and your respectful & relevant comments!