Vectors in R Programming and types of vectors
What are Vectors?
Fundamental Data Structure: Vectors in R are the most basic and fundamental data structure. They are one-dimensional arrays that store elements of a single data type.
Homogeneous: All the elements within a vector must be of the same type (e.g., all numbers, all characters, etc.).
Types of Vectors
R supports six atomic vector types:
Numeric: Stores numbers (integers or doubles, i.e., decimals).
Example: my_numeric <- c(1, 5, 2.7, 10)
Integer: Stores whole numbers only.
Example: my_integer <- c(2L, 6L, 15L) (the 'L' signifies integer)
Logical: Stores Boolean values (TRUE or FALSE).
Example: my_logical <- c(TRUE, FALSE, TRUE)
Character: Stores text or strings.
Example: my_character <- c("hello", "world", "data")
Complex: Stores complex numbers (numbers with real and imaginary parts).
Example: my_complex <- c(2+5i, 3-1i)
Raw: Stores raw bytes of data (this type is less commonly used directly).
Creating Vectors
The primary way to create vectors in R is using the c() function (short for "combine"):
Code snippet
# Create a numeric vector
numbers <- c(5, 2, 8.3, 1)
# Create a character vector
greetings <- c("hello", "good morning")
Operations on Vectors
Accessing Elements: Access specific elements using square brackets [] and their index (position). Indexing starts from 1.
Code snippet
numbers[2] # Accesses the second elementArithmetic Operations: Perform element-wise addition, subtraction, multiplication, division, etc.
Code snippet
numbers * 2 # Multiply each element by 2Logical Comparisons: Perform element-wise comparisons (<, >, ==, etc.).
Code snippet
numbers > 4 # Check which elements are greater than 4Functions: Apply various built-in functions to vectors.
Code snippet
length(numbers) # Find the length (number of elements)
sum(numbers) # Calculate the sum
sort(numbers) # Sort in ascending order
Key Points
Vectors are an essential building block in R, used extensively in data analysis and manipulation.
Understanding different vector types ensures you store and work with your data correctly.
R provides a rich set of functions and operations for working with vectors.
1. Deleting an Entire Vector
To completely remove a vector from your R workspace, use the rm() function:
Code snippet
my_vector <- c(10, 20, 30) # Create a vector
rm(my_vector) # Remove the vector
Vectors in R Programming and types of vectors¶
my_numeric <- c(1, 5, 2.7, 10)
my_numeric
- 1
- 5
- 2.7
- 10
my_numeric1 = c(1, 5, 2.7, 10)
my_numeric1
- 1
- 5
- 2.7
- 10
my_integer <- c(2L, 6L, 15L)
my_integer
- 2
- 6
- 15
my_integer <- c(2L, 6.5L, 15L)
my_integer
- 2
- 6
- 15
typeof(my_integer)
my_integer <- c(2L, 6L, 15L)
typeof(my_integer)
typeof(my_integer)
my_integer <- c(2L, 6.5L, 15L)
typeof(my_integer)
age <- c(10,20,30,'40')
age
- '10'
- '20'
- '30'
- '40'
typeof(age)
age <- c(10,20,30,40)
typeof(age)
my_logical <- c(TRUE, FALSE, TRUE)
typeof(my_logical)
my_logical <- c(TRUE, FALSE, TRUE,20)
typeof(my_logical)
my_complex <- c(2+5i, 3-1i)
typeof(my_complex)
rm(my_complex)
my_complex
Error in eval(expr, envir, enclos): object 'my_complex' not found Traceback:
No comments:
Post a Comment
Note: only a member of this blog may post a comment.