*38*

There are two common ways to create a vector of ones in R:

**Method 1: Use c()**

#create vector of 12 ones ones_vector

**Method 2: Use rep()**

#create vector of 12 ones ones_vector

The following examples show how to use each method in practice.

**Related:** How to Create a Vector of Zeros in R

**Example 1: Create Vector of Ones Using c()**

The following code shows how to create a vector of ones using the **c()** function:

#create vector of 12 ones ones_vector #view vector ones_vector [1] 1 1 1 1 1 1 1 1 1 1 1 1

The result is a vector with 12 ones.

This method is easy to use but it can be cumbersome if you want to create an extremely long vector.

For example, if you wanted to create a vector with 100 ones then it would take a long time to type out each individual value.

**Example 2: Create Vector of Ones Using rep()**

The following code shows how to create a vector of ones using the **rep()** function:

#create vector of 12 ones ones_vector #view vector ones_vector [1] 1 1 1 1 1 1 1 1 1 1 1 1

The result is a vector with 12 ones.

Note that this method is much more efficient if you want to create an extremely long vector because you only have to provide two arguments to the **rep()** function.

The first argument specifies the value to replicate and the second value specifies the number of times to replicate it.

**Related:** How to Use rep() Function in R to Replicate Elements

**Additional Resources**

The following tutorials explain how to perform other common tasks in R:

How to Create a Vector with Random Numbers in R

How to Create an Empty Vector in R

How to Check if a Vector Contains a Given Element in R