# Vectors in R (part 1)

# Vectors

R is a vector‐based language. You can think of a vector as a row or column of numeric data, character data, or logical data. The list of numbers {1,2,3,4,5}, for example, could be a vector. Unlike most other programming languages, R allows you to apply functions to the whole vector in a single operation without the need for an explicit loop.

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] 5

x <- 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 7

y <- 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?

Elements of a vector can be accessed in three different ways:

- 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 10

x[3] # access 3rd element

[1] 4

x[c(2, 4)] # access 2nd and 4th element

[1] 2 6

x[2:4] # access from 2nd to 4th element

[1] 2 4 6

x[-1] # access all but 1st element

[1] 2 4 6 8 10

x[c(2, -4)] # cannot mix positive and negative integers

Error in x[c(2, -4)] : only 0's may be mixed with negative subscripts

x[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 3

x[x < 0] # filtering vectors based on conditions

[1] -3 -1

x[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 modify a vector using the assignment operator.

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 2

x[2] <- 0; x # modify 2nd element

[1] -3 0 -1 0 1 2

x[x<0] <- 5; x # modify elements less than 0

[1] 5 0 5 0 1 2

x <- x[1:4]; x # truncate x to first 4 elements

[1] 5 0 5 0

# How to delete a Vector?

We can delete a vector by simply assigning a `NULL`

to it.

`x`

[1] -3 -2 -1 0 1 2

x <- NULL

x

NULL

x[4]

NULL

# Operations in R Vector

**Combining Vector in R**

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”)]