R Vectors

Used to store an ordered collection of elements of the same data type.
There are two function which decalres a vector
  1. c() Function
    Syntax - c(...)
    Concatenates elements into a vector.
    Example: numeric_vector <- c(1, 2, 3, 4, 5)
  2. vector() Function
    Syntax - vector(mode, length)
    Creates a vector of a specified mode and length.
    Example: logical_vector <- vector("logical", length = 3)

Example

# Using c() function
numeric_vector <- c(1, 2, 3, 4, 5)
character_vector <- c("apple", "banana", "orange")

# Using vector() function
logical_vector <- vector("logical", length = 3)
logical_vector[1] <- TRUE
logical_vector[2] <- FALSE
logical_vector[3] <- TRUE

Accessing Elements

We can access the vector with index number inside a [] bracket.

Example

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

# Accessing elements of a vector
first_element <- numeric_vector[1]  # Access the first element
second_element <- character_vector[2]  # Access the second element

Vector Operations and functions

Operators are used in vectors to perform various operations.
Functions are a built in methods that performs operations fast and easy.

Operation

Syntax

Description

Example

Addition

vec1 + vec2

Element-wise addition of two vectors.

c(1, 2, 3) + c(4, 5, 6)

Subtraction

vec1 - vec2

Element-wise subtraction of one vector from another.

c(4, 5, 6) - c(1, 2, 3)

Multiplication

vec * scalar or vec1 * vec2

Scalar multiplication or element-wise multiplication.

c(1, 2, 3) * 2

Division

vec / scalar or vec1 / vec2

Scalar division or element-wise division.

c(4, 5, 6) / 2

Exponentiation

vec^exp

Element-wise exponentiation.

c(1, 2, 3)^2

Concatenation

c(vec1, vec2)

Concatenates two or more vectors.

c(1, 2, 3, 4, 5, 6)

Sum

sum(vec)

Calculates the sum of vector elements.

sum(c(1, 2, 3))

Mean

mean(vec)

Calculates the mean (average) of vector elements.

mean(c(1, 2, 3))

Length

length(vec)

Returns the number of elements in a vector.

length(c(1, 2, 3))

Square Root

sqrt(vec)

Calculates the square root of each element.

sqrt(c(1, 4, 9))

Vectorized Functions

vec1^2, sqrt(vec)

Applying functions to each element vectorized.

c(1, 2, 3)^2, sqrt(c(1, 4, 9))


Example

# Creating vectors
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)

# Vector addition
result_addition <- vec1 + vec2
print("Vector Addition:")
print(result_addition)  # Output: 5 7 9

# Scalar multiplication
result_scalar_multiplication <- vec1 * 2
print("\nScalar Multiplication:")
print(result_scalar_multiplication)  # Output: 2 4 6

# Element-wise exponentiation
result_exponentiation <- vec1^2
print("\nElement-wise Exponentiation:")
print(result_exponentiation)  # Output: 1 4 9

# Concatenation
combined_vector <- c(vec1, vec2)
print("\nConcatenation:")
print(combined_vector)  # Output: 1 2 3 4 5 6

# Sum and Mean
sum_of_vector <- sum(vec1)
mean_of_vector <- mean(vec1)
print("\nSum of Vector:", sum_of_vector)  # Output: 6
print("Mean of Vector:", mean_of_vector)  # Output: 2

# Applying a function to each element vectorized
square_root_vector <- sqrt(vec1)
print("\nSquare Root of Vector:")
print(square_root_vector)  # Output: 1 1.414214 1.732051

Quick Recap - Topics Covered

R Vectors
Vector Operations and functions

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.


Example js

Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK