Create a numeric vector in R
Last Updated :
18 Apr, 2024
A one-dimensional array containing numerical data is called a numeric vector in R Programming Language. Numerical values, such as integers or real numbers, are often stored and manipulated using numerical vectors.
Concepts related to the topic
- Numerical Data Types: Real and integer numbers may be represented in R using numeric data types. These numeric data types may be stored in numerical vectors.
- Vector Creation: Numerical vectors may be generated directly using numeric literals or with the use of methods such as c(), seq(), and rep().
- Vector Operations: Mathematical operations like addition, subtraction, multiplication, and division are supported by numerical vectors.
Steps required to create a numeric vector in R
- Choose the numbers that you want to have in the vector.
- To generate the vector, use the relevant functions, such as rep(), seq(), or c().
- You may also choose to apply any required adjustments or modifications to the vector.
- Utilize the vector as required in your R code to do further computations or analysis.
Creating a Numeric Vector using c( )
R
# Create a numeric vector using c()
numeric_vector <- c(1, 2, 3, 4, 5)
numeric_vector
Output:
[1] 1 2 3 4 5
Generating a Sequence of Numbers using seq( )
R
# Generate a sequence of numbers
numeric_sequence <- seq(from = 1, to = 10, by = 2)
numeric_sequence
Output:
[1] 1 3 5 7 9
Creating a Replicated Numeric Vector using rep( )
R
# Create a replicated numeric vector
replicated_vector <- rep(5, times = 3)
replicated_vector
Output:
[1] 5 5 5
Share your thoughts in the comments
Please Login to comment...