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

Vectors in R

Vectors are one of the fundamental data structures in R. They are ordered collections of elements, all of the same type. In this tutorial, we'll explore how to work with vectors in R.

1. Creating Vectors

Vectors are created using the c() function, which stands for 'concatenate' or 'combine'.

1.1 Basic Vectors

# Numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)

# Character vector
char_vector <- c("A", "B", "C")

# Logical vector
logical_vector <- c(TRUE, FALSE, TRUE)

1.2 Using Variables in Vectors

You can use variables when creating vectors.

x <- 5
y <- 10
vector <- c(x, y)  # This will create a vector with values 5 and 10

2. Accessing Vector Elements

You can access vector elements using their indices.

2.1 Indexing Starts at 1

In R, indexing starts at 1, not 0 as in many other programming languages.

numeric_vector <- c(10, 20, 30, 40)
print(numeric_vector[1])  # Outputs: 10

2.2 Negative Indices

Using negative indices will exclude the corresponding element.

print(numeric_vector[-1])  # Outputs all elements except the first one

2.3 Multiple Indices

You can access multiple elements by providing multiple indices.

print(numeric_vector[c(1,3)])  # Outputs: 10 30

3. Vector Operations

3.1 Basic Arithmetic

You can perform element-wise arithmetic on vectors.

a <- c(1, 2, 3)
b <- c(4, 5, 6)
sum_vector <- a + b  # Outputs: 5 7 9

3.2 Logical Operations

Logical operations are also element-wise.

c <- c(TRUE, FALSE, TRUE)
d <- c(FALSE, FALSE, TRUE)
result <- c & d  # Outputs: FALSE FALSE TRUE

4. Functions with Vectors

R has a plethora of built-in functions that can be applied to vectors.

4.1 Length of a Vector

length_vector <- c(10, 20, 30, 40, 50)
print(length(length_vector))  # Outputs: 5

4.2 Sum and Mean

print(sum(length_vector))  # Outputs: 150
print(mean(length_vector))  # Outputs: 30

4.3 Sorting

unsorted_vector <- c(20, 5, 15, 10)
sorted_vector <- sort(unsorted_vector)
print(sorted_vector)  # Outputs: 5 10 15 20

5. Vector Sequences

You can generate sequences using the : operator or the seq() function.

# Using : operator
seq1 <- 1:5  # Outputs: 1 2 3 4 5

# Using seq function
seq2 <- seq(from = 1, to = 10, by = 2)  # Outputs: 1 3 5 7 9

Conclusion

Vectors are foundational in R and provide a versatile tool for data manipulation and statistical analysis. With a good understanding of vectors, you're well on your way to becoming proficient in R. Remember, to get the most out of R, it's essential to have a solid grasp of vector operations and the built-in functions that R provides for them.

  1. Introduction to vectors in R:

    • Vectors are one-dimensional arrays that store elements of the same data type.
    # Example of vector creation
    numeric_vector <- c(1, 2, 3, 4, 5)
    
  2. Creating vectors in R:

    • Use the c() function to create vectors by combining elements.
    # Example of creating vectors
    numeric_vector <- c(1, 2, 3, 4, 5)
    character_vector <- c("apple", "banana", "orange")
    
  3. R vector operations:

    • Perform operations on vectors element-wise.
    # Example of vector operations
    x <- c(1, 2, 3)
    y <- c(4, 5, 6)
    sum_result <- x + y
    
  4. Subsetting vectors in R:

    • Access specific elements or subsets of vectors.
    # Example of subsetting vectors
    numeric_vector <- c(1, 2, 3, 4, 5)
    subset_result <- numeric_vector[2:4]
    
  5. Vector functions in R:

    • Use functions like length(), mean(), and others to perform operations on vectors.
    # Example of vector functions
    numeric_vector <- c(1, 2, 3, 4, 5)
    vector_length <- length(numeric_vector)
    mean_value <- mean(numeric_vector)
    
  6. R vectorization:

    • Vectorization is a key concept in R, allowing operations on entire vectors without explicit loops.
    # Example of vectorization
    x <- c(1, 2, 3)
    squared_vector <- x^2
    
  7. Concatenating vectors in R:

    • Combine vectors using functions like c() or append().
    # Example of concatenating vectors
    vector1 <- c(1, 2, 3)
    vector2 <- c(4, 5, 6)
    concatenated_vector <- c(vector1, vector2)
    
  8. Vector indexing in R:

    • Use indexing to access and modify specific elements of a vector.
    # Example of vector indexing
    numeric_vector <- c(10, 20, 30, 40, 50)
    numeric_vector[3] <- 35
    
  9. Numeric vectors in R:

    • Vectors can store numeric values.
    # Example of numeric vectors
    numeric_vector <- c(1.5, 2.5, 3.5)
    
  10. Character vectors in R:

    • Vectors can store character strings.
    # Example of character vectors
    character_vector <- c("apple", "banana", "orange")
    
  11. Logical vectors in R:

    • Vectors can store logical (Boolean) values.
    # Example of logical vectors
    logical_vector <- c(TRUE, FALSE, TRUE)
    
  12. Vector arithmetic in R:

    • Perform arithmetic operations on vectors.
    # Example of vector arithmetic
    x <- c(1, 2, 3)
    y <- c(4, 5, 6)
    sum_result <- x + y
    
  13. Vector manipulation in R:

    • Manipulate vectors using various functions and operations.
    # Example of vector manipulation
    numeric_vector <- c(1, 2, 3, 4, 5)
    squared_vector <- numeric_vector^2
    
  14. R vector examples:

    • Combine concepts to solve specific problems or tasks.
    # Example of vector usage
    ages <- c(25, 30, 35, 40, 45)
    average_age <- mean(ages)