Data in R is held as a wide variety of objects such as vectors, matrices, arrays, data frames, and lists. Let’s see How to create or assign a vector in R.

#### What is Vector?

Vectors are one-dimensional arrays that hold numeric data, character data, or logical data. The combine function `c()`

is used to create a vector.

**Examples**

a <- c(5, 10, 15, 20, 25, -20, -15) b <- c("Toyota", "Suzuki", "Maruti", "BMW", "Audi") c <- c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)

Here, `a`

is a numeric vector, `b`

is a character vector, and `c`

is a logical vector. You have to note that the data in a vector must only be one type or mode (numeric, character, or logical). You canâ€™t mix data type in the same vector.

You can refer to elements of a vector using a numeric vector of positions within brackets. For example, `a[c(2, 4,6 )]`

refers to the 2nd, 4th and 6th element of the vector `a`

.

##### Examples

a <- c(5, 10, 15, 20, 25, -20, -15) a[4] [1] 20 a[c(2,4,6)] [1] 10 20 -20

The colon operator shall be used to generate a sequence of numbers.

##### Example

a <- c(2:6) a <- c(2, 3, 4, 5, 6).

#### How to use Vector in R?

I will show you some simple examples here on how to use a vector in R.

The following example will show you. What to do if you want to add 5 to everything in this vector, or to square each entry.

> a <- c(1:10) > a [1] 1 2 3 4 5 6 7 8 9 10 > a+5 [1] 6 7 8 9 10 11 12 13 14 15 > a^2 [1] 1 4 9 16 25 36 49 64 81 100

Now let us see how to find mean

> mean(a) [1] 5.5

In next post will see how to use matrices.