# Vectors

First, assign the values 1:5 to a vector called x:

`x <-1:5x[1] 1 2 3 4 5`

Next, add the value 2 to each element in the vector x:

`x + 2[1] 3 4 5 6 7`

You can also add one vector to another. To add the values 6:10 element‐ wise to x, you do the following:

`x +6:10[1] 7 9 11 13 15`

Create a vector

In R, you create a vector with the combine function `c().` You place the vector elements separated by a comma between the parentheses

`numeric_vector <- c(1, 2, 3)character_vector <- c("a", "b", "c")`

Since, a vector must have elements of the same type, this function will try and coerce elements to the same type, if they are different.

Coercion is from lower to higher types from logical to integer to double to character.

`x <- c(1, 5, 4, 9, 0)typeof(x)[1] "double"length(x)[1] 5x <- c(1, 5.4, TRUE, "hello")x[1] "1"     "5.4"   "TRUE"  "hello"typeof(x)[1] "character"`

If we want to create a vector of consecutive numbers, the `:` operator is very helpful.

`x <- 1:7; x[1] 1 2 3 4 5 6 7y <- 2:-2; y[1]  2  1  0 -1 -2`

More complex sequences can be created using the `seq()` function, like defining number of points in an interval, or the step size.

`> seq(1, 3, by=0.2)          # specify step size[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0> seq(1, 5, length.out=4)    # specify length of the vector[1] 1.000000 2.333333 3.666667 5.000000`

# How to access Elements of a Vector?

• Using integer vector as index
• Using logical vector as index
• Using character vector as indez

Using integer vector as index

Vector index in R starts from 1, unlike most programming languages where index start from 0.

We can use a vector of integers as index to access specific elements.

We can also use negative integers to return all elements except that those specified.

`x[1]  0  2  4  6  8 10x[3]           # access 3rd element[1] 4x[c(2, 4)]     # access 2nd and 4th element[1] 2 6x[2:4]     # access from 2nd to 4th element[1] 2 4 6x[-1]          # access all but 1st element[1]  2  4  6  8 10x[c(2, -4)]    # cannot mix positive and negative integersError in x[c(2, -4)] : only 0's may be mixed with negative subscriptsx[c(2.4, 3.54)]    # real numbers are truncated to integers[1] 2 4`

Using logical vector as index

When we use a logical vector for indexing, the position where the logical vector is `TRUE` is returned.

This useful feature helps us in filtering of vector as shown below.

`x[c(TRUE, FALSE, FALSE, TRUE)][1] -3  3x[x < 0]  # filtering vectors based on conditions[1] -3 -1x[x > 0][1] 3`

In the above example, the expression `x>0` will yield a logical vector `(FALSE, FALSE, FALSE, TRUE)` which is then used for indexing.

Using character vector as index

This type of indexing is useful when dealing with named vectors. We can name each elements of a vector.

`x <- c("first"=3, "second"=0, "third"=9)names(x)[1] "first"  "second" "third" x["second"]second 0 x[c("first", "third")]first third 3     9`

Selection by comparisson

`c(4, 5, 6) > 5[1] FALSE FALSE TRUE`

# How to modify a vector in R?

We can use the techniques discussed above to access specific elements and modify them.

If we want to truncate the elements, we can use reassignments.

`x[1] -3 -2 -1  0  1  2x[2] <- 0; x        # modify 2nd element[1] -3  0 -1  0  1  2x[x<0] <- 5; x   # modify elements less than 0[1] 5 0 5 0 1 2x <- x[1:4]; x      # truncate x to first 4 elements[1] 5 0 5 0`

# How to delete a Vector?

`x[1] -3 -2 -1  0  1  2x <- NULLxNULLx[4]NULL`

# Operations in R Vector

In order to combine the two vectors in R, we will create two new vectors ’n’ and ‘s’. Then, we will create another vector that will combine these two using c(n,s) as follows:

For example:

`n = c(1, 2, 3, 4)s = c(“Hadoop”, “Spark”, “HIVE”, “Flink”)c(n,s)[1] '1' '2' '3' '4' 'Hadoop' 'Spark' 'HIVE' 'Flink'`

Arithmetic Operations

Arithmetic operations on vectors can be performed member-by-member.

For example:

Suppose we have two vectors a and b:

`a <- c (1, 3)b <- c (1, 3)a + b #Addition[1] 2 6a — b #Subtraction[1] 0 0a / b #Division[1] 1 1a %% b #Remainder Operation[1] 0 0`

Duplicate Index

The index vector allows duplicate values. Hence, the following retrieves a member twice in one operation.

For example:

`s = c(“aa”, “bb”, “cc”, “dd”, “ee”)s[c(2,3,3)][1] ‘bb’ ‘cc’ ‘cc’`

Named Vectors Members

We first create our vector of characters:

`v = c(“Hadoop”, “Spark”)v`

Then, we name the first vector member as “First” and the second member as “Second”.

`names(v) = c(“First”, “Second”)v`

We retrieve the first member by its name as follows:

`v[“First”]`

We can also reverse the order using the character string index vector:

`v[c(“Second”, “First”)]`

## Elemental Chat is live!

Get the Medium app