Open In App

Create a numeric vector in R

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. 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.
  2. Vector Creation: Numerical vectors may be generated directly using numeric literals or with the use of methods such as c(), seq(), and rep().
  3. Vector Operations: Mathematical operations like addition, subtraction, multiplication, and division are supported by numerical vectors.

Steps required to create a numeric vector in R

  1. Choose the numbers that you want to have in the vector.
  2. To generate the vector, use the relevant functions, such as rep(), seq(), or c().
  3. You may also choose to apply any required adjustments or modifications to the vector.
  4. 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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads