Types of Vectors in R Programming

Types of Vectors in detail

Vectors in R programming are similar to arrays in C language and are used to hold multiple data values of the same type. One key difference is that in R, the indexing of vectors starts from 1 instead of 0. Vectors are the most fundamental data types in R. Even a single object is stored in the form of a vector. Vectors are essentially arrays and contain a sequence of homogeneous data types.

There are two main types of vectors in R:

  1. Atomic Vector
    1. Integer
    2. Double
    3. Logical
    4. Character
    5. Complex
    6. Raw
  2. Recursive Vector
    • List

The primary difference between atomic vectors and recursive vectors (lists) is that atomic vectors are homogeneous, while recursive vectors (lists) can be heterogeneous.

Vectors in R have three common properties:

  • Type: What it is, determined using the typeof() function.
  • Length: Number of elements it contains, determined using the length() function.
  • Attributes: Additional metadata, accessed using the attributes() function.
1. Atomic Vectors

i. Integer Vector

Integer vectors store whole numbers (positive or negative). In R, numbers are double by default, so to explicitly create an integer, append L after the number.

Example:

# Creating an integer vector
v1 <- c(10L, -3L, 7L, 22L)

# Printing the vector
print(v1)

# Displaying the type of vector
print(typeof(v1))

Output:

[1] 10 -3  7 22
[1] "integer"

ii. Double Vector: Double vectors include numbers with decimal values. In R, numbers are double by default.

Example:

# Creating a double vector
v1 <- c(3.14, 5.67, -2.18, 0.99)

# Printing the vector
print(v1)

# Displaying the type of vector
print(typeof(v1))

Output:

[1]  3.14  5.67 -2.18  0.99
[1] "double"

iii. Logical Vector: Logical vectors can have three possible values: TRUEFALSE, and NA. They are often created using comparison operators or the c() function.

Example:

# Creating a logical vector
v1 <- c(TRUE, FALSE, TRUE, NA)

# Printing the vector
print(v1)

# Displaying the type of vector
print(typeof(v1))

Output:

[1]  TRUE FALSE  TRUE    NA
[1] "logical"

iv Character Vector: Character vectors store strings. Each element of a character vector is a string, which can include alphabets, numbers, and symbols. Values are represented in quotes.

Example:

# Creating a character vector
v1 <- c("apple", "42", "hello", "99.9")

# Printing the vector
print(v1)

# Displaying the type of vector
print(typeof(v1))

Output:

[1] "apple" "42"    "hello" "99.9"
[1] "character"

v. Complex Vector: Complex vectors store numbers with an imaginary component. Examples include 3+4i or -7i.

Example:

# Creating a complex vector
v1 <- c(2+3i, -1-2i, 0+4i, 7-3i)

# Printing the vector
print(v1)

# Displaying the type of vector
print(typeof(v1))

Output:

[1]  2+3i -1-2i  0+4i  7-3i
[1] "complex"

vi. Raw Vector: Raw vectors store raw bytes of data and can be created using the raw() function.

Example:

[1] "1" "2" "3" "a" "b" "c"

Output:

[1] 00 00 00 00
[1] "raw"
2. Recursive Vector (List)

Lists are more flexible than atomic vectors because they can hold elements of different types, including other lists. Lists are often used to represent hierarchical or tree-like data structures.

Example:

# Creating a list (recursive vector)
l1 <- list("text", 42, TRUE, c(3, 4, 5))

# Printing the list
print(l1)

# Displaying the type of the list
print(typeof(l1))

Output:

[[1]]
[1] "text"

[[2]]
[1] 42

[[3]]
[1] TRUE

[[4]]
[1] 3 4 5

[1] "list"

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *