How do you create an empty vector in R? Can you provide examples?

How do you create an empty vector in R? Can you provide examples?

To create an empty vector in R, you can use the function “vector()” and specify the length and type of the vector. For example, to create an empty vector of length 5 with numeric values, you would use the code “vector(mode = “numeric”, length = 5)”. Similarly, to create an empty vector with character values, you would use “vector(mode = “character”, length = 5)”. Another way to create an empty vector in R is by using the shorthand notation “numeric(0)” or “character(0)”. These methods will create an empty vector of the specified length and type, ready to be filled with data.

Create an Empty Vector in R (With Examples)


You can use one of the following methods to create an empty vector in R:

#create empty vector with length zero and no specific class
empty_vec <- vector()

#create empty vector with length zero and a specific class
empty_vec <- character()

#create empty vector with specific length
empty_vec <- rep(NA, times=10)

The following examples show how to use each of these methods in practice.

Method 1: Create Empty Vector with Length Zero

The following code shows how to create a vector with a length of zero and no specific class:

#create empty vector with length zero and no specific class
empty_vec <- vector()

#display length of vector
length(empty_vec)

[1] 0

We can then fill the vector with values if we’d like:

#add values 1 through 10 to empty vector
empty_vec <- c(empty_vec, 1:10)

#view updated vector
empty_vec

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

Method 2: Create Empty Vector of Specific Class

The following code shows how to create empty vectors of specific classes:

#create empty vector of class 'character'empty_vec <- character()

class(empty_vec)[1] "character"

#create empty vector of class 'numeric'
empty_vec <- numeric()

class(empty_vec)

numeric(0)

#create empty vector of class 'logical'
empty_vec <- logical()

class(empty_vec)

logical(0)

Method 3: Create Empty Vector with Specific Length

The following code shows how to create a vector with a specific length in R:

#create empty vector with length 10
empty_vec <- rep(NA, times=10)

#display empty vector
empty_vec

[1] NA NA NA NA NA NA NA NA NA NA

If you know the length of the vector at the outset, this is the most memory-efficient solution in R.

Cite this article

stats writer (2024). How do you create an empty vector in R? Can you provide examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-create-an-empty-vector-in-r-can-you-provide-examples/

stats writer. "How do you create an empty vector in R? Can you provide examples?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-do-you-create-an-empty-vector-in-r-can-you-provide-examples/.

stats writer. "How do you create an empty vector in R? Can you provide examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-create-an-empty-vector-in-r-can-you-provide-examples/.

stats writer (2024) 'How do you create an empty vector in R? Can you provide examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-create-an-empty-vector-in-r-can-you-provide-examples/.

[1] stats writer, "How do you create an empty vector in R? Can you provide examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How do you create an empty vector in R? Can you provide examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top