5 Ways to Create a Vector (and Fill it With Elements) in R

5 ways to create a vector in R

A vector is one of the most commonly used data structures in R. This tutorial explains five ways to create a vector and fill it with elements in R.

Option 1: Specify Each Element in Vector

The most straightforward way to create a vector in R is by specifying the value of each element in the vector:

#create a vector x with 10 elements
x <- c(14, 16, 27, 38, 35, 12, 2, 7, 4, 13)

While this is a simple way to create a vector, it can become tedious if you have hundreds of elements to manually type in.

Option 2: Create a Vector Using : Operator

Another way to create a vector in R is by using the : operator, which allows you to create a list of elements based on a starting and ending number or character.

#create a vector x that contains the numbers from 1 to 10
x <- c(1:10)
x

#[1] 1 2 3 4 5 6 7 8 9 10

#create a vector x that contains the numbers from 10 to 1
x <- c(10:1)
x

#[1] 10 9 8 7 6 5 4 3 2 1

#create a vector x that contains the numbers from -2 to 4
x <- c(-2:4)
x

#[1] -2 -1 0 1 2 3 4

#create a vector x that contains the uppercase letters from A to D
x <- LETTERS[1:4]
x

#[1] "A" "B" "C" "D"

#create a vector x that contains the lowercase letters from w to z
x <- letters[23:26]
x

#[1] "w" "x" "y" "z"

Using the : operator can be a convenient way to create a vector and fill it with elements if your list of elements has a starting and ending value.

Option 3: Create a Vector Using seq() Function

Another way to create a vector in R is by using the seq() function, which allows you to generate a sequence of numbers based on a starting and ending value.

#create a vector x that contains values from 0 to 20 by increments of 4.
x <- seq(0, 20, by = 4)
x

#[1]  0  4  8  12  16  20

#create a vector x that contains values 3 equally spaced values from 0 to 20
x <- seq(0, 20, length.out = 3)
x

#[1] 0 10 20

Option 4: Create a Vector Using rep() Function

Yet another way to create a vector in R is by using the rep() function, which allows you to create a vector by replicating values a certain number of times.

#create a vector x by replicating the number "4" ten times
x <- rep(4, 10)
x

#[1] 4 4 4 4 4 4 4 4 4 4

#create a vector x by replicating the series (4, 5) ten times
x <- rep(4:5, 10)
x

#[1] 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5

#create a vector x by replicating the series ("A", "B", "C") three times
x <- rep(LETTERS[1:3], 3)
x

#[1] "A" "B" "C" "A" "B" "C" "A" "B" "C"

#create a vector x by replicating each value in the series (1, 2, 3) two times
x <- rep(1:3, each = 2)
x

#[1] 1 1 2 2 3 3

Option 5: Create a Vector By Generating Random Variables

Another way to create a vector is by generating random variables.

#create a vector of 10 random variables that are uniformly distributed with
#minimum value 4 and maximum value 20
x <- runif(10, min = 4, max = 20)
x

#[1] 14.334988 6.351954 10.000329 15.124179 15.786767 18.269868 15.803017
#[8] 6.665265 11.753413 15.392658

#create a vector of 10 random variables that are uniformly distributed with
#minimum value 4 and maximum value 20 and round each value to 0 decimal places
x <- round(runif(10, min = 4, max = 20), 0)
x

#[1] 18 15 13 16 10 10 9 14 13 9

#create a vector of 10 random variables that are normally distributed with
#mean = 10 and standard deviation = 5
x <- rnorm(10, mean = 10, sd = 5)
x

#[1] 16.190987 10.507228 10.802308 7.772253 2.675838 13.863898 5.394071
#[8] 13.173717 11.281625 19.978385

#create a vector of 10 random variables that are normally distributed with
#mean = 10 and standard deviation = 5 and round each value to 0 decimal places
x <- round(rnorm(10, mean = 10, sd = 5), 0)
x

#[1] 5 3 1 10 15 10 8 7 5 2

In the two examples above, we used the runif() function to generate uniformally distributed random variables and the rnorm() function to generate normally distributed random variables.

However, there are many functions in R to generate a list of random variables that follow various distributions including the binomial distribution, exponential distribution, and many others.

These functions are particularly helpful when you want to generate lists of random variables that follow particular statistical distributions.

Leave a Reply

Your email address will not be published. Required fields are marked *