0% found this document useful (0 votes)
0 views23 pages

R Programming Notes

The document provides an overview of vectors in R, detailing their types, creation methods, and operations. It covers numeric, character, logical, integer, and complex vectors, along with examples of vector creation and manipulation. Additionally, it discusses vector operations, including arithmetic, relational, logical operations, and functions for sequences, repetition, sorting, and lengths.

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views23 pages

R Programming Notes

The document provides an overview of vectors in R, detailing their types, creation methods, and operations. It covers numeric, character, logical, integer, and complex vectors, along with examples of vector creation and manipulation. Additionally, it discusses vector operations, including arithmetic, relational, logical operations, and functions for sequences, repetition, sorting, and lengths.

Uploaded by

pankaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Confidential - Oracle Restricted

Vectors
Vectors are a fundamental data structure in R used to store sequences of elements that are
all of the same type. They are essential for data manipulation and statistical analysis.

Types of Vectors in R

1. Numeric Vector: Contains numbers (integers or floating-point).

2. Character Vector: Contains strings or text data.

3. Logical Vector: Contains TRUE or FALSE values.

4. Integer Vector: A special type of numeric vector where all elements are integers.

5. Complex Vector: Contains complex numbers with real and imaginary parts.

Creating and Using Vectors

Vectors can be created using the c() function, which combines or concatenates elements
into a vector.

Examples of Each Type:

1. Numeric Vector

num_vec <- c(1.5, 2.3, 4.7)

print(num_vec) # Outputs: [1] 1.5 2.3 4.7

c means concatenate

2. Character Vector

char_vec <- c("apple", "banana", "cherry")

print(char_vec) # Outputs: [1] "apple" "banana" "cherry"

3. Logical Vector

log_vec <- c(TRUE, FALSE, TRUE)

print(log_vec) # Outputs: [1] TRUE FALSE TRUE

Confidential - Oracle Restricted


Confidential - Oracle Restricted

4. Integer Vector

o You can create integer vectors by appending L to numbers or using


as.integer().

int_vec <- c(1L, 2L, 3L)

print(int_vec) # Outputs: [1] 1 2 3

class(int_vec) # Outputs: "integer"

5. Complex Vector

complex_vec <- c(1+2i, 3-4i, 5+0i)

print(complex_vec) # Outputs: [1] 1+2i 3-4i 5+0i

Simple Program Demonstrating Different Vector Types

# Create vectors of different types

num_vec <- c(10.5, 20.3, 30.8) # Numeric vector

char_vec <- c("R", "is", "fun") # Character vector

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

int_vec <- c(5L, 10L, 15L) # Integer vector

complex_vec <- c(2+3i, 4-2i) # Complex vector

# Print each vector

print("Numeric Vector:")

print(num_vec) # Outputs: [1] 10.5 20.3 30.8

print("Character Vector:")

print(char_vec) # Outputs: [1] "R" "is" "fun"

Confidential - Oracle Restricted


Confidential - Oracle Restricted

print("Logical Vector:")

print(log_vec) # Outputs: [1] TRUE FALSE TRUE

print("Integer Vector:")

print(int_vec) # Outputs: [1] 5 10 15

print("Complex Vector:")

print(complex_vec) # Outputs: [1] 2+3i 4-2i

Vector Operations

You can perform operations on vectors, and they apply element-wise:

vec1 <- c(1, 2, 3)

vec2 <- c(4, 5, 6)

sum_vec <- vec1 + vec2 # Outputs: [1] 5 7 9

Summary

• Vectors are the simplest data structures in R and can only hold elements of the
same type.

• Types include numeric, character, logical, integer, and complex.

• Creation is done using the c() function, and operations on vectors apply to each
element.

Vector operations in R are powerful because they are performed element-wise,


meaning that operations are automatically applied to each element of the vectors.
Below are the main types of vector operations, along with examples to illustrate
their use:

1. Arithmetic Operations

These operations can be performed directly on numeric vectors:

• Addition (+)

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Subtraction (-)

• Multiplication (*)

• Division (/)

• Exponentiation (^)

Example:

Copy code

vec1 <- c(2, 4, 6)

vec2 <- c(1, 3, 5)

# Element-wise addition

result_add <- vec1 + vec2 # Outputs: [1] 3 7 11

# Element-wise subtraction

result_sub <- vec1 - vec2 # Outputs: [1] 1 1 1

# Element-wise multiplication

result_mul <- vec1 * vec2 # Outputs: [1] 2 12 30

# Element-wise division

result_div <- vec1 / vec2 # Outputs: [1] 2.000000 1.333333 1.200000

# Element-wise exponentiation

result_exp <- vec1 ^ vec2 # Outputs: [1] 2^1 4^3 6^5 -> [1] 2 64 7776

2. Relational Operations

These operations return logical vectors indicating the comparison results:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Greater than (>)

• Less than (<)

• Equal to (==)

• Not equal to (!=)

• Greater than or equal to (>=)

• Less than or equal to (<=)

Example:

Copy code

vec1 <- c(5, 10, 15)

vec2 <- c(3, 10, 20)

# Greater than comparison

result_gt <- vec1 > vec2 # Outputs: [1] TRUE FALSE FALSE

# Equal to comparison

result_eq <- vec1 == vec2 # Outputs: [1] FALSE TRUE FALSE

3. Logical Operations

These operations work with logical vectors and return logical results:

• AND (&)

• OR (|)

• NOT (!)

Example:

Copy code

vec1 <- c(TRUE, FALSE, TRUE)

Confidential - Oracle Restricted


Confidential - Oracle Restricted

vec2 <- c(FALSE, FALSE, TRUE)

# Logical AND

result_and <- vec1 & vec2 # Outputs: [1] FALSE FALSE TRUE

# Logical OR

result_or <- vec1 | vec2 # Outputs: [1] TRUE FALSE TRUE

# Logical NOT

result_not <- !vec1 # Outputs: [1] FALSE TRUE FALSE

4. Vector Recycling

If two vectors of different lengths are used in an operation, R recycles the shorter
vector to match the length of the longer one.

Example:

Copy code

vec1 <- c(1, 2, 3, 4)

vec2 <- c(5, 10)

# Recycling occurs; `vec2` becomes c(5, 10, 5, 10)

result_recycle <- vec1 + vec2 # Outputs: [1] 6 12 8 14

5. Element Access and Manipulation

You can access or modify vector elements using indexing.

• Single element access: vec[2]

• Range access: vec[1:3]

• Conditional access: vec[vec > 5]

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Example:

Copy code

vec <- c(10, 20, 30, 40, 50)

# Access the third element

element <- vec[3] # Outputs: 30

# Access a range of elements

range_elements <- vec[2:4] # Outputs: [1] 20 30 40

# Conditional access

greater_than_25 <- vec[vec > 25] # Outputs: [1] 30 40 50

6. Common Vector Functions

• sum(): Sum of all elements.

• mean(): Average of elements.

• max() and min(): Maximum and minimum elements.

• length(): Number of elements in the vector.

Example:

Copy code

vec <- c(5, 10, 15, 20)

# Sum of elements

total_sum <- sum(vec) # Outputs: 50

Confidential - Oracle Restricted


Confidential - Oracle Restricted

# Mean of elements

average <- mean(vec) # Outputs: 12.5

# Maximum element

max_value <- max(vec) # Outputs: 20

# Length of the vector

vec_length <- length(vec) # Outputs: 4

Summary

• Vector operations in R are applied element-wise and support arithmetic, relational,


and logical operations.

• Recycling allows for operations on vectors of different lengths but may need
attention to avoid unexpected results.

• Element access and functions like sum(), mean(), max(), and min() are useful for
vector manipulation and analysis.

4o

Sequences, Repetition, Sorting, and Lengths

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Sequences, repetition, sorting, and lengths are common operations and


functions in R that are useful for data manipulation and analysis. Here’s an
explanation of each concept with examples:
1. Sequences
Sequences are ordered sets of numbers. You can create sequences in R using
functions like seq() or the colon operator :.
Examples:
• Using the Colon Operator:
seq1 <- 1:10 # Creates a sequence from 1 to 10
print(seq1) # Outputs: [1] 1 2 3 4 5 6 7 8 9 10

• Using seq() Function:


seq2 <- seq(from = 5, to = 20, by = 3) # Creates a sequence from 5 to 20 with a
step of 3
print(seq2) # Outputs: [1] 5 8 11 14 17 20
seq3 <- seq(from = 1, to = 10, length.out = 5) # Generates 5 equally spaced
numbers between 1 and 10
print(seq3) # Outputs: [1] 1.00 3.25 5.50 7.75 10.00
2. Repetition
Repetition is useful when you need to create vectors with repeated values.
The rep() function is used for this.
Examples:
• Repeating a Single Value:
rep1 <- rep(5, times = 4) # Repeats the number 5 four times
print(rep1) # Outputs: [1] 5 5 5 5

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Repeating a Vector:

rep2 <- rep(c(1, 2, 3), times = 2) # Repeats the vector (1, 2, 3) two times
print(rep2) # Outputs: [1] 1 2 3 1 2 3
• Repeating Each Element:
rep3 <- rep(c(1, 2, 3), each = 3) # Repeats each element of the vector three
times
print(rep3) # Outputs: [1] 1 1 1 2 2 2 3 3 3
3. Sorting
Sorting is used to arrange the elements of a vector in ascending or descending
order. The sort() function is commonly used for this purpose.
Examples:
• Ascending Order:
vec <- c(10, 3, 8, 1, 5)
sorted_vec <- sort(vec) # Sorts in ascending order by default
print(sorted_vec) # Outputs: [1] 1 3 5 8 10
• Descending Order:
sorted_vec_desc <- sort(vec, decreasing = TRUE) # Sorts in descending order
print(sorted_vec_desc) # Outputs: [1] 10 8 5 3 1

4. Lengths
The length() function is used to determine the number of elements in a vector
or list.
Examples:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Finding the Length of a Vector:


vec <- c(4, 7, 1, 9, 3)
len <- length(vec) # Returns the number of elements in the vector
print(len) # Outputs: 5
• Finding Length of an Empty Vector:
empty_vec <- c()
print(length(empty_vec)) # Outputs: 0
Summary
• Sequences can be created with : or seq() for ordered sets.
• Repetition of elements or vectors can be done using rep().
• Sorting organizes data in ascending or descending order with sort().
• Lengths of vectors or lists are determined using length().
These operations are fundamental for data manipulation and help in preparing
and exploring datasets in R.

Subsetting and element extraction


Subsetting and element extraction are essential techniques in R for accessing
specific parts of vectors, matrices, lists, and data frames. They allow you to
retrieve, modify, or manipulate data efficiently. Here’s how these operations
are performed:
1. Subsetting Vectors
Subsetting a vector means accessing one or more elements from it. There are
different ways to subset vectors:
• Using Positive Indexing: Extracts elements at specific positions.
• Using Negative Indexing: Excludes elements at specific positions.

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Using Logical Vectors: Selects elements that meet certain conditions.


• Using Names: Extracts elements by their names if they are named.
Examples:
• Positive Indexing:

vec <- c(10, 20, 30, 40, 50)


subset_vec <- vec[2] # Extracts the second element
print(subset_vec) # Outputs: 20
• Negative Indexing:
subset_vec_neg <- vec[-3] # Excludes the third element
print(subset_vec_neg) # Outputs: [1] 10 20 40 50
• Logical Indexing:
subset_vec_logical <- vec[vec > 25] # Extracts elements greater than 25
print(subset_vec_logical) # Outputs: [1] 30 40 50
• Named Indexing:
names(vec) <- c("a", "b", "c", "d", "e")
subset_named <- vec["c"] # Extracts the element named "c"
print(subset_named) # Outputs: 30

Matrices

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Matrices in R are two-dimensional data structures that store elements of the


same type (typically numeric or character data). They are arranged in rows and
columns and are useful for mathematical computations and data analysis.
Here's an overview of matrices, including how to create, access, and
manipulate them.
1. Creating Matrices
Matrices can be created using the matrix() function or by combining vectors.
Examples:
• Using the matrix() Function:
mat <- matrix(1:9, nrow = 3, ncol = 3)
print(mat)
# Outputs:
# [,1] [,2] [,3]
# [1,] 1 4 7
# [2,] 2 5 8
# [3,] 3 6 9
• Specifying byrow = TRUE: By default, matrices are filled column-wise.
You can fill them row-wise by setting byrow = TRUE.
mat_by_row <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
print(mat_by_row)
# Outputs:
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
# [3,] 7 8 9

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Creating a Matrix by Combining Vectors:


vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
combined_mat <- rbind(vec1, vec2) # Creates a matrix by binding rows
print(combined_mat)
# Outputs:
# [,1] [,2] [,3]
# vec1 1 2 3
# vec2 4 5 6
2. Accessing Matrix Elements
You can access specific elements, rows, or columns using indices.
Examples:
• Accessing a Specific Element:
mat <- matrix(1:9, nrow = 3)
elem <- mat[2, 3] # Extracts the element in the 2nd row, 3rd column
print(elem) # Outputs: 8
• Accessing Entire Rows or Columns:
row_extract <- mat[1, ] # Extracts the first row
print(row_extract) # Outputs: [1] 1 4 7

col_extract <- mat[, 2] # Extracts the second column


print(col_extract) # Outputs: [1] 4 5 6
3. Matrix Operations

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Matrices support various mathematical operations, including addition,


subtraction, multiplication, and transposition.
Examples:
• Matrix Addition and Subtraction:
mat1 <- matrix(1:4, nrow = 2)
mat2 <- matrix(5:8, nrow = 2)
sum_mat <- mat1 + mat2
print(sum_mat)
# Outputs:
# [,1] [,2]
# [1,] 6 10
# [2,] 8 12
• Matrix Multiplication: Use %*% for matrix multiplication.
mat_mult <- mat1 %*% mat2
print(mat_mult)
# Outputs (result depends on matrix dimensions):
# [,1] [,2]
# [1,] 19 22
# [2,] 43 50
• Element-wise Multiplication: Use * for element-wise multiplication.
elementwise_mult <- mat1 * mat2
print(elementwise_mult)
# Outputs:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

# [,1] [,2]
# [1,] 5 21
# [2,] 12 32
• Transposing a Matrix:

transposed_mat <- t(mat1)


print(transposed_mat)
# Outputs:
# [,1] [,2]
# [1,] 1 3
# [2,] 2 4
4. Functions for Matrices
• dim(): Returns the dimensions (rows and columns) of the matrix.
• nrow() and ncol(): Return the number of rows and columns,
respectively.
• diag(): Creates or extracts the diagonal of a matrix.
• solve(): Computes the inverse of a matrix (if invertible).
Examples:
• Dimensions and Diagonal:
print(dim(mat)) # Outputs: [1] 3 3 (3 rows, 3 columns)
print(diag(mat)) # Outputs: [1] 1 5 9

5. Combining Matrices

Confidential - Oracle Restricted


Confidential - Oracle Restricted

You can combine matrices by rows or columns using rbind() and cbind().
Example:
mat1 <- matrix(1:4, nrow = 2)
mat2 <- matrix(5:8, nrow = 2)
combined_row <- rbind(mat1, mat2)
print(combined_row)
# Outputs:
# [,1] [,2]
# [1,] 1 3
# [2,] 2 4
# [3,] 5 7
# [4,] 6 8

combined_col <- cbind(mat1, mat2)


print(combined_col)
# Outputs:
# [,1] [,2] [,3] [,4]
# [1,] 1 3 5 7
# [2,] 2 4 6 8

Summary

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Matrices are two-dimensional data structures with rows and columns.


• Creation can be done using matrix() or by combining vectors.
• Accessing elements uses [row, column] notation.
• Operations include addition, multiplication, and transposition.
• Functions like dim(), diag(), and solve() are useful for matrix analysis.

ARRAY
Arrays in R are multi-dimensional data structures that can hold data of the
same type (usually numeric or character). Unlike matrices, which are limited
to two dimensions, arrays can have more than two dimensions, making them
suitable for more complex data structures.
1. Creating Arrays
You can create arrays in R using the array() function, specifying the data and
dimensions.
Syntax:
array(c(rows,columns,layers).
array(data, dim = c(dim1, dim2, dim3, ...))
• data: The elements to populate the array.
• dim: A vector specifying the number of elements in each dimension.
Example:
arr <- array(1:12, dim = c(3, 4))
print(arr)
# Outputs a 3x4 matrix:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

# [,1] [,2] [,3] [,4]


# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12
Example of a 3D Array:
arr_3d <- array(1:24, dim = c(3, 4, 2))
print(arr_3d)
# Outputs a 3-dimensional array with two 3x4 matrices:
#,,1
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12

#,,2
# [,1] [,2] [,3] [,4]
# [1,] 13 16 19 22
# [2,] 14 17 20 23
# [3,] 15 18 21 24

2. Accessing Elements in Arrays

Confidential - Oracle Restricted


Confidential - Oracle Restricted

You can access elements of an array using indices for each dimension.
Example:
• Accessing a Single Element:
elem <- arr_3d[2, 3, 1] # Accesses the element at the 2nd row, 3rd column,
1st matrix
print(elem) # Outputs: 8
• Accessing an Entire Row or Column:
row_extract <- arr_3d[1, , 2] # Extracts the 1st row from the 2nd matrix
print(row_extract) # Outputs: [1] 13 16 19 22

col_extract <- arr_3d[, 4, 1] # Extracts the 4th column from the 1st matrix
print(col_extract) # Outputs: [1] 10 11 12
• Accessing an Entire Matrix:
matrix_extract <- arr_3d[, , 1] # Extracts the 1st matrix
print(matrix_extract)
# Outputs:
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12
3. Modifying Elements
You can modify elements in an array by assigning new values to specific
positions.
Example:

Confidential - Oracle Restricted


Confidential - Oracle Restricted

arr_3d[2, 2, 1] <- 99 # Modifies the element at the 2nd row, 2nd column, 1st
matrix
print(arr_3d[2, , 1]) # Outputs the modified 2nd row of the 1st matrix: [1] 2 99 8
11
4. Array Operations
Arrays support arithmetic operations, which are performed element-wise.
Examples:
• Addition:
arr1 <- array(1:6, dim = c(2, 3))
arr2 <- array(7:12, dim = c(2, 3))
arr_sum <- arr1 + arr2
print(arr_sum)
# Outputs:
# [,1] [,2] [,3]
# [1,] 8 12 16
# [2,] 10 14 18
• Multiplication:
arr_mult <- arr1 * arr2
print(arr_mult)
# Outputs:
# [,1] [,2] [,3]
# [1,] 7 27 55
# [2,] 16 40 72
5. Checking Array Properties

Confidential - Oracle Restricted


Confidential - Oracle Restricted

Arrays have certain properties that you can check using various functions:
• dim(): Returns the dimensions of the array.
• length(): Returns the total number of elements in the array.
• dimnames(): Assigns or retrieves names for the dimensions.
Examples:

print(dim(arr_3d)) # Outputs: [1] 3 4 2 (3 rows, 4 columns, 2 matrices)


print(length(arr_3d)) # Outputs: 24 (total number of elements)

# Assigning names to dimensions:


dimnames(arr_3d) <- list(c("Row1", "Row2", "Row3"), c("Col1", "Col2", "Col3",
"Col4"), c("Matrix1", "Matrix2"))
print(dimnames(arr_3d))
6. Combining Arrays
Arrays can be combined along existing or new dimensions using functions like
abind() (requires the abind package) or manually combining using cbind() and
rbind() if dimensions allow.
Example:
# Combining arrays along the third dimension
library(abind)
combined_arr <- abind(arr1, arr2, along = 3)
print(dim(combined_arr)) # Outputs: [1] 2 3 2 (2 rows, 3 columns, 2 matrices)
Summary

Confidential - Oracle Restricted


Confidential - Oracle Restricted

• Arrays in R can be multi-dimensional, supporting data storage beyond


2D matrices.
• Creation is done with array() by specifying data and dimensions.
• Element Access uses indexing for each dimension.
• Arithmetic Operations are element-wise and can involve more than
one array.
• Properties can be checked using dim(), length(), and dimnames().
when working with multi-dimensional datasets.

Confidential - Oracle Restricted

You might also like