## Introduction to R Vectors

Vectors are basic R data type objects. They are divided into six atomic vectors.

### Single Element Vectors

The data created with one value is of the vector of length 1.

- character

**Example:** print(“xyz”)

- Integer

**Example:** print(54L)

- Double

**Example:** print(6.5)

- Complex

**Example: **print(45L)

- logical

**Example:** print(FALSE)

- Raw

**Example:** print(1+3i)

### Multiple Element Vector

We can also create a vector with multiple values using the colon with numerical data.

**Example **

`v <- 5:10`

print(v)

**output**: 5 6 7 8 9 10

The sequence of decimal values

`v <- 6.6:9.6`

print(v)

**output**: 6.6 7.6 8.6 9.6

In the below example,11.4 will not be in the sequence and it will be discarded as it doesn’t belong in the sequence.

`v <- 3.8:8.4`

print(v)

**output**: 3.8 4.8 5.8 6.8 7.8

### Vector Functions in R

The function is defined as a piece of code used to perform a task. Functions are treated as other types of objects. There different types of functions in R programming.

#### 1. R rep() function

As we can refer from the name, this function is used to repeat the values given as an input in a function.

4.5 (2,181 ratings)

View Course

**Syntax:** `rep()`

**Example **

`rep(c(1, 2, 3), times = 4)`

**Output:**

** **[1] 1 2 3 1 2 3 1 2 3 1 2 3

** **There are different ways of implementing the rep function.

** **We can also input the number of times a particular value needs to be repeated.

**Example**

`rep(c(1, 2), times = c(4,3))`

**Output:**

The third way of implementing a rep function is by specifying the length.

**Example**

`rep(1:2,length.out=9)`

**Output:**

#### 2. R Seq function

The sequence function is used to create a set of sequential values.

Let’s suppose we want to create a set of sequential integers. We can use the sequence function to create them.

**Syntax**: `seq()`

**Example:**

`seq(from = 3.5, to = 1, by = -0.3)`

**Output:**

Additionally, We can also add another attribute to the input of the function (i.e length)

**Example**

`seq(from = 3.5, to = 1, length.out = 6)`

**Output;**

** **[1] 3.5 3.0 2.5 2.0 1.5 1.0

#### 3. R any() function

Any() function takes input values and returns logical vectors which either True or False.

**Syntax** : `any()`

Let’s assume we have assigned a set of values to x.

x = 1,2,3,4,5

**Syntax**:

`x <- 1:5`

Now, we use any() function to see if there is any value above 5.

`any(x > 5)`

**Output**:

FALSE From a similar example, We can also output TRUE by changing the value from 5 to 3.

any(x > 3)

**Output:**

TRUE

#### 4. R all() function

The all() function is similar to any() function.The difference here is it checks for every value and then prints the output. Even if a single value doesn’t meet the condition specified, it will print as FALSE.

**Syntax: **`all()`

**Example**

**1. When all values meet the condition**

`x <- 1:5all(x > 0)`

**Output:** TRUE** **** **

**2.When values don’t meet the condition**

`x <- 1:5all(x > 3)`

**Output:** FALSE

In the above example, only two values are above 3. The functions in R is defined by Rf_ or R_ ** **** ** ** **** **

### Recommended Articles

This is a guide to R Vectors. Here we discuss the different types of Vector functions in R programming with Syntax and Examples. You may also have a look at the following articles to learn more –