R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Types of Vectors in R

In R, vectors are the most basic data structures and play a central role. They are one-dimensional arrays that can hold numeric data, character data, or logical data. The data in vectors are known as elements.

In this tutorial, we'll discuss the different types of vectors available in R:

1. Atomic Vectors:

There are four primary types of atomic vectors, depending on the type of data they hold.

a. Numeric Vectors:

Used for storing numeric values.

numeric_vector <- c(1.5, 2.5, 3.5, 4.5)
print(numeric_vector)

b. Integer Vectors:

Used for storing integer values. To ensure the vector is of integer type, you can use the L suffix.

integer_vector <- c(1L, 2L, 3L, 4L)
print(integer_vector)

c. Logical Vectors:

Used for storing boolean values: TRUE, FALSE, and NA (for missing data).

logical_vector <- c(TRUE, FALSE, TRUE, TRUE)
print(logical_vector)

d. Character Vectors:

Used for storing strings.

character_vector <- c("apple", "banana", "cherry")
print(character_vector)

2. Complex Vectors:

These are used for storing complex numbers.

complex_vector <- c(1+2i, 2+3i)
print(complex_vector)

3. List:

While atomic vectors are homogeneous (all elements are of the same type), lists are heterogeneous. A list can have elements of different types, even other lists.

my_list <- list(1:3, "apple", c(TRUE, FALSE), list("a", "b"))
print(my_list)

4. Factors:

Factors are used to store categorical data. They can be thought of as integer vectors where each integer has a label.

gender <- c("male", "female", "female", "male", "male")
factor_gender <- factor(gender)
print(factor_gender)

5. Raw Vectors:

These are used to store raw bytes.

raw_vector <- charToRaw("Hello, R!")
print(raw_vector)

Vector Operations:

R offers a wide variety of operations that can be performed on vectors:

  • Arithmetic Operations: These can be performed on numeric vectors.
v1 <- c(1, 2, 3)
v2 <- c(4, 5, 6)
print(v1 + v2)  # Adds the vectors element-wise
  • Logical Operations: These can be applied to logical vectors.
v1 <- c(TRUE, FALSE, TRUE)
v2 <- c(FALSE, FALSE, TRUE)
print(v1 & v2)  # Element-wise logical AND
  • Character Operations: You can use functions like paste() for string concatenation.
str1 <- c("Hello", "R")
print(paste(str1, collapse = " "))

Conclusion:

Vectors are foundational in R, forming the basis for more advanced data structures like data frames and matrices. Understanding how to create, manipulate, and use the different types of vectors is essential for anyone working with R.

  1. Atomic Vectors in R:

    • Atomic vectors are basic data structures holding elements of the same data type.
    # Example: Atomic vector
    numeric_vector <- c(1, 2, 3, 4, 5)
    
  2. Numeric Vectors in R:

    • Numeric vectors store numerical values.
    # Example: Numeric vector
    numeric_vector <- c(1, 2, 3, 4, 5)
    
  3. Character Vectors in R:

    • Character vectors store text or strings.
    # Example: Character vector
    character_vector <- c("apple", "orange", "banana")
    
  4. Logical Vectors in R:

    • Logical vectors store TRUE or FALSE values.
    # Example: Logical vector
    logical_vector <- c(TRUE, FALSE, TRUE)
    
  5. Factor Vectors in R:

    • Factor vectors represent categorical variables with predefined levels.
    # Example: Factor vector
    factor_vector <- factor(c("low", "medium", "high"), levels = c("low", "medium", "high"))
    
  6. Complex Vectors in R:

    • Complex vectors store complex numbers.
    # Example: Complex vector
    complex_vector <- c(1 + 2i, 3 + 4i, 5 + 6i)
    
  7. List Vectors in R:

    • List vectors can hold elements of different data types.
    # Example: List vector
    list_vector <- list(1, "apple", TRUE)
    
  8. Vector Coercion in R:

    • Coercion occurs when combining different types into a vector.
    # Example: Vector coercion
    combined_vector <- c(1, "apple", TRUE)
    
  9. Creating Vectors of Different Types in R:

    • Create vectors with a mix of numeric, character, and logical elements.
    # Example: Mixed type vector
    mixed_vector <- c(1, "apple", TRUE)
    
  10. Operations on Numeric Vectors in R:

    • Perform arithmetic operations on numeric vectors.
    # Example: Arithmetic operations on numeric vector
    result_vector <- numeric_vector * 2
    
  11. Handling Missing Values in Vectors in R:

    • Use NA to represent missing values in vectors.
    # Example: Handling missing values in vectors
    missing_vector <- c(1, NA, 3, 4, 5)
    
  12. Vector Indexing in R:

    • Access specific elements in a vector using indexing.
    # Example: Vector indexing
    third_element <- numeric_vector[3]
    
  13. Concatenating Vectors in R:

    • Combine vectors using functions like c().
    # Example: Concatenating vectors
    combined_vector <- c(numeric_vector, character_vector)
    
  14. R Vector Types Comparison:

    • Compare different vector types using functions like is.numeric().
    # Example: Vector types comparison
    is_numeric <- is.numeric(numeric_vector)