Assigning Vectors in detail
Vectors are one of the fundamental data structures in R. They store data of the same type and are equivalent to arrays in many other programming languages. In R, an array is essentially a vector with one or more dimensions, and almost every object created in R is stored as a vector. The elements of a vector are referred to as its components.
Assigning Vectors
There are multiple ways to assign values to vectors in R. This can be achieved using the c() function, the : operator, or the seq() function.
Assigning Vectors Using c():
The c() function is commonly used to create vectors in R.
Example 1:
# Creating a vector using c()
numbers = c(10, 20, 30, 40, 50)
# Printing the vector
print(numbers)
# Printing the data type of the vector
print(typeof(numbers))
Output:
[1] 10 20 30 40 50
[1] "double"
Example 2:
# Creating a mixed vector
mixed_vector = c(3.2, FALSE, 8, "LearnR")
# Printing the vector
print(mixed_vector)
# Printing the data type of the vector
print(typeof(mixed_vector))
Output:
[1] "3.2" "FALSE" "8" "LearnR"
[1] "character"
Assigning Vectors Using :
The : operator is used to create vectors of consecutive values.
Example 1:
# Creating a vector of consecutive integers
sequence = 5:15
# Printing the vector
print(sequence)
Output:
[1] 5 6 7 8 9 10 11 12 13 14 15
Example 2:
# Creating a vector of consecutive decimal numbers
decimal_sequence = 2.1:6.1
# Printing the vector
print(decimal_sequence)
Output:
[1] 2.1 3.1 4.1 5.1 6.1
Example 3:
# Creating a vector with mismatched intervals
incomplete_sequence = 2.1:6.9
# Printing the vector
print(incomplete_sequence)
Output:
[1] 2.1 3.1 4.1 5.1 6.1
Assigning Vectors Using seq()
The seq() function allows the creation of vectors with custom step sizes.
Example 1:
# Creating a vector using seq() with a custom step size
stepped_vector = seq(2, 10, by = 1.5)
# Printing the vector
print(stepped_vector)
Output:
[1] 2.0 3.5 5.0 6.5 8.0 9.5
Example 2:
You can also specify the desired length of the vector, and R will calculate the step size automatically.
# Creating a vector with a specified length
length_based_vector = seq(1, 20, length.out = 5)
# Printing the vector
print(length_based_vector)
Output:
[1] 1.00 5.75 10.50 15.25 20.00
Assigning Named Vectors in R
R allows the creation of named vectors where each value is associated with a specific name. The names() function can be used for this purpose.
Example:
Suppose we want to create a vector representing the number of team members in different departments.
# Creating a numeric vector for team sizes
team_sizes = c(4, 6, 10, 12)
# Assigning department names
names(team_sizes) = c("HR", "Finance", "Development", "Operations")
# Displaying the named vector
print(team_sizes)
Output:
HR Finance Development Operations
4 6 10 12
To access a department with a specific team size:
# Displaying the department with 10 team members
print(names(team_sizes[team_sizes == 10]))
# Displaying the department with 3 team members
print(names(team_sizes[team_sizes == 3]))
Output:
[1] "Development"
character(0)
Accessing Elements of a Vector
Vector indexing in R is used to access individual elements or subsets of a vector. Note that indexing in R starts from 1, not 0.
Example 1:
# Creating a vector using seq()
values = seq(10, 100, by = 10)
# Printing the vector
print(values)
# Accessing the fourth element
print(values[4])
Output:
[1] 10 20 30 40 50 60 70 80 90 100
[1] 40
Example 2:
# Accessing multiple elements
print(values[c(2, 5, 8)])
Output:
[1] 20 50 80
Leave a Reply