R Vectors
R Vectors
Tutorial Name: Codes With Pankaj Website: www.codeswithpankaj.com
Table of Contents
Introduction to Vectors in R
Creating Vectors
Using
c()
FunctionUsing
seq()
andrep()
Functions
Accessing Elements in a Vector
Vector Operations
Arithmetic Operations
Relational Operations
Logical Operations
Vector Functions
length()
sum()
mean()
sort()
Modifying Vectors
Adding Elements
Removing Elements
Updating Elements
Named Vectors
Combining Vectors
Vector Coercion
1. Introduction to Vectors in R
Vectors are one of the most basic and essential data structures in R. A vector is a sequence of data elements that are of the same type. Vectors can hold numeric, character, logical, or complex data types. In R, almost everything is a vector, making it a fundamental concept for R programming.
2. Creating Vectors
2.1 Using c()
Function
The simplest way to create a vector in R is by using the c()
(combine) function.
Example:
2.2 Using seq()
and rep()
Functions
seq()
: Creates a sequence of numbers.rep()
: Repeats elements of a vector.
Example:
3. Accessing Elements in a Vector
You can access elements in a vector using square brackets []
. R uses 1-based indexing, meaning the first element is at position 1.
Example:
4. Vector Operations
4.1 Arithmetic Operations
You can perform arithmetic operations on vectors, and the operations are applied element-wise.
Example:
4.2 Relational Operations
Relational operations are also applied element-wise and return a logical vector.
Example:
4.3 Logical Operations
Logical operations can be used to combine conditions across vector elements.
Example:
5. Vector Functions
5.1 length()
The length()
function returns the number of elements in a vector.
Example:
5.2 sum()
The sum()
function returns the sum of all elements in a numeric vector.
Example:
5.3 mean()
The mean()
function returns the average of the elements in a numeric vector.
Example:
5.4 sort()
The sort()
function sorts the elements of a vector in ascending or descending order.
Example:
6. Modifying Vectors
6.1 Adding Elements
You can add elements to a vector using the c()
function.
Example:
6.2 Removing Elements
To remove elements from a vector, you can use negative indexing.
Example:
6.3 Updating Elements
You can update elements in a vector by directly assigning new values to specific positions.
Example:
7. Named Vectors
You can assign names to the elements of a vector, making it easier to reference specific elements.
Example:
8. Combining Vectors
You can combine vectors using the c()
function to create larger vectors.
Example:
9. Vector Coercion
If you create a vector with elements of different types, R will automatically coerce them into a common type. The hierarchy of coercion is logical -> integer -> numeric -> character.
Example:
Conclusion
Vectors are a fundamental data structure in R, and mastering their creation, manipulation, and operations is essential for effective R programming. Whether you're working with numeric, logical, or character data, vectors provide a flexible and powerful way to store and manipulate data.
For more tutorials and resources, visit Codes With Pankaj at www.codeswithpankaj.com.
Last updated