We share R tutorials from scientists at academic and scientific institutions with a goal to give everyone in the world access to a free knowledge. Our tutorials cover different topics including statistics, data manipulation and visualization!

Introduction

Vectors can be manipulated by adding other elements such as numbers and characters on specific positions of the vector. In example below we add the element `11`

on the 4th position of the vector.

Insert 11 after 4th position of the vector

x <- c(2,7,12,25,9) x <- c(x[[1:4]],11,x[[4:5]]) x2 7 12 25 11 25 9

Also, you can add or subtract from elements of vectors. For example, if you want to add `2`

value in whole vector or on specific elements of vectors do as follow.

Add 1 on each element

x <- c(2,7,12,25,9) x + 24 9 14 27 14 28 12

Add 2 on specific elements of vector

x[[c(2,3)]]+29 14

The functions `all()`

and `any()`

could be used with the vectors. In some cases when the vector is large and contain many elements you want to know whether any or all of their elements are `TRUE`

.

For example, you have a numeric vector `x`

which contain 10 numbers from 1 to 10, use the function `any(x==5)`

to know if any of numbers is `5`

, or `all(x==5)`

if all numbers in vector are `5`

.

Numeric vector from 1 to 10

x<-c(1:10)

Is any of numbers 5

any(x==5)TRUE

Are all numbers 5

all(x==5)FALSE

The `subset()`

, `which()`

and `ifelse()`

are probably the most commonly used functions in R.

One way to filter elements in the vector is to use `subset()`

function.

Let's first create vector `x`

.

x <- c(5, 4:8, 12) x5 4 5 6 7 8 12

Now we want to subset elements which are smaller then 6.

y <- subset(x, x < 6) y5 4 5

Now let use the function `which()`

to identify a position in the vector.

which(x > 8)12

The function `ifelse`

has two statements to execute. If condition is `TRUE`

the first statement is executed. If condition is `FALSE`

, the second statement is executed.

For example if x < 6 make 1, otherwise make 0.

ifelse(x < 6, 1, 0)1 1 1 0 0 0 0

Also you can do something like this:

ifelse(x < 6, 1, x)1 1 1 6 7 8 12

When you work with vectors you might be interested to identify an element or a set of elements within the vector. For example you want to show the element in the 2nd and 5th position of the vector. The code used is `x[[c(,)]]`

. Follow the example below.

Create a numeric vector:

x <- c(1, 5, 7, 2, 3, -4, 12)

Identify 2nd and 5th position

x[[c(2,5)]]5 3

Identify from 2nd position up to 5

x[[c(2:5)]]5 7 2 3

To assess row and columns in matrices use this function `x[row,column]`

. In example below I will create a matrix by using the function `rnorm()`

which is a function to create random numbers from normal distribution.

Let create a matrix with 12 random numbers in 4 rows

x <- matrix(rnorm(12), nrow=4) x[,1] [,2] [,3] [1,] -0.9930798 0.1125335 -0.5039369 [2,] 0.4793895 -0.8137567 -0.6098343 [3,] -0.2082529 0.2085910 -0.9223005 [4,] -0.1511682 -0.1885532 0.6812309

Now we want to find the number in 3rd row and 2nd column

x[3,2]0.208591

Also, you can show entire column or row. Using the matrix from example above we now identify entire second column or row.

x[,2]0.1125335 -0.8137567 0.2085910 -0.1885532

For the rows we use code below:

x[4,]-0.1511682 -0.1885532 0.6812309

Another interesting function `dim()`

which is used to identify the number of rows and columns in matrices.

dim(x)4 3

Adding elements to the list is similar as you will add variables in the data frame. However, today we will show how to add elements in the list. Elements can be numbers or character. You can add more than one element in the list.

First let create a sample list:

y <- list(job="researcher", age="55") y$job "researcher" $age "55"

Now lets add another element to current list:

y$name <- "Andre" y$job "researcher" $age "55" $name "Andre"

Let see now how you can delete an element by using function `NULL`

from the first sample list.

y$age <- NULL y$job "researcher"

If you have any question related to vectors please post a comment below.