How to create a numeric vector in R ? You can use the c()
function or numeric()
function to create a numeric vector. A numeric vector is a type of vector that only contains values of numeric type. The numeric() function creates a vector with a specified length of zeros whereas c() creates a vector with the specified values, if all values are numbers then it creates a numeric vector.
In this article, I will explain how to create numeric vectors and how to implement them using the c() function and numeric() function.
Key points-
- Numeric vectors in R can contain only numeric values.
- The
c()
function is used to combine elements to create a numeric vector. numeric()
function creates an empty numeric vector or a vector with a specified length of zeros.- Use
as.numeric()
function converts any type of vector to numeric. - Elements of a numeric vector can be accessed using bracket notation.
- Using the assignment operator you can update elements of a numeric vector.
- Arithmetic operations can be performed element-wise on numeric vectors.
- The length of a numeric vector represents the number of elements it contains.
Create a Numeric Vector in R
In R, c()
function is used to create a numeric vector by combining all elements of numeric. After creating a numerical vector you can use the class()
function to check the class of the vector. Let’s pass the numerical values into a c() function to create a numeric vector.
# Create numeric Vector using c()
num_vec <- c(1, 3, 5, 7, 9)
print("Create a numeric vector:")
print(num_vec)
print("Type of numeric vector:")
class(num_vec)
Yields below output.
Create an Empty Numeric Vector
The numeric()
function creates an empty numeric vector in R without specifying the length. The following example initializes an empty numeric vector without passing any length. The number of elements in a vector represents the length of the vector. The length of an empty numeric vector is ‘0’.
# Create empty numeric vector
num_vec <- numeric()
print("Create empty numeric vector:")
print(num_vec)
print("Length of empty numeric vector:")
length(num_vec)
Yields below output.
Create a Numeric Vector of Specified Length
The numeric()
function creates a numeric vector of a specified length. All elements of the vector will contain a specified length of zeros(0). The following example initializes a numeric vector of length 5 with all zeros.
# Create numeric vector of specified length
num_vec <- numeric(5)
print("Create numeric vector of specified length:")
print(num_vec)
print("Length of numeric vector:")
length(num_vec)
# Output:
# [1] "Create numeric vector of specified length:"
# [1] 0 0 0 0 0
# [1] "Length of numeric vector:"
# [1] 5
Convert Character Vector to Numeric Vector
If you have a character vector, and you want to convert it to a numeric vector by using the as.numeric()
function. This function is used to convert the vector of any type to the numeric type. This function takes the values of the character and creates a numeric vector.
# Convert to numeric vector
num_vec <- as.numeric("1", "2","3")
class(num_vec)
# Output:
# > class(num_vec)
# [1] "numeric"
Extract the Numeric Vector Element
To extract an element from a vector, you can use the bracket ([]
) notation of R. To specify the index of the element that you want to extract from the given vector inside the bracket notation followed by the vector name. It will return the element of the corresponding specified index of the given vector.
# Extract the specified elements of numeric vectors\
print("Get first element of numeric vector:")
num_vec[1]
print("Get third element of numeric vector:")
num_vec[3]
# Output:
# [1] "Get first element of numeric vector:"
# [1] 1
# [1] "Get third element of numeric vector:"
# [1] 5
Update the Element of Numeric Vector
You can update the elements of a numeric vector by using the assignment operator. To do this, specify the index of the element you want to replace inside the bracket notation([]), followed by the vector. Then, on the right side of the assignment operator(=), include the new element that you want to replace the previous one with. This process will update the vector’s elements with the specified new elements.
print("Update the first element of numeric vector:")
num_vec[1] <- 2
print("Update the third element of numeric vector:")
num_vec[3] <- 4
print("After updating the vector:")
num_vec
# Output:
# [1] "Update the first element of numeric vector:"
# > num_vec[1] <- 2
# [1] "Update the third element of numeric vector:"
# > num_vec[3] <- 4
# [1] "After updating the vector:"
# [1] 2 3 4 7 9
As you can see from the above, the first and third elements of num_vec
are changed to 2
and 4
.
Operations Between Two Numeric Vectors
As we know numeric vectors are created using numbers, so you can perform arithmetic operations in numeric vectors. Let’s create two vectors of the same length, and perform arithmetic operations on vectors elementwisely.
# Apply arithematic operation on numeric vectors
print("First numeric vector:")
num_vec1 <- c(1, 3, 5, 7, 9)
num_vec1
print("Second numeric vector:")
num_vec2 <- c(2, 4, 6, 8, 9)
num_vec2
print("After applying arithmatic operations:")
num_vec1 + num_vec2
num_vec1 - num_vec2
num_vec1 * num_vec2
num_vec1 / num_vec2
# Output:
# [1] "First numeric vector:"
# > num_vec1
# [1] 1 3 5 7 9
# [1] "Second numeric vector:"
# > num_vec2
# [1] 2 4 6 8 9
# [1] "After applying arithmatic operations:"
# > num_vec1 + num_vec2
# [1] 3 7 11 15 18
# > num_vec1 - num_vec2
# [1] -1 -1 -1 -1 0
# > num_vec1 * num_vec2
# [1] 2 12 30 56 81
# > num_vec1 / num_vec2
# [1] 0.5000000 0.7500000 0.8333333 0.8750000 1.0000000
Conclusion
In this article, I have explained how to create numeric vectors using the c() function and numeric() function. Also explained how to implement them using bracket notation([]) and using the as.numeric() function how to convert particular data types to numerical type.
Happy Learning!!
Related Articles
- Explain the character vector in R
- How to Convert List to Vector in R?
- How to Concatenate Vector in R?
- Merge Vector Elements into String in R?
- How to Subset Vector in R?
- How to Sort Vector in R?
- How to Convert List to String?
- Explain merge() function in R