Vectors can be manipulated by inserting other elements, such as numbers or characters, at specific positions of the vector. In the example below, we insert the element 11 after the 4th position of the vector.
Insert 11 after the 4th position of the vector:
x <- c(2,7,12,25,9) x <- c(x[1:4],11,x[5]) x 2 7 12 25 11 9
You can also add to or subtract from the elements of a vector. For example, if you want to add the value 2 to the whole vector, or only to specific elements, do as follows.
Add 2 to each element:
x <- c(2,7,12,25,9) x + 2 4 9 14 27 11
Add 2 to specific elements of the vector:
x[c(2,3)]+2 9 14
all() and any() functions
The functions all() and any() can be used with vectors. In some cases, when the vector is large and contains many elements, you may want to know whether any or all of its elements satisfy a condition.
For example, suppose you have a numeric vector x which contains the 10 numbers from 1 to 10. Use the function any(x==5) to check whether any of the numbers is 5, or all(x==5) to check whether all the numbers in the vector are 5.
Create a numeric vector from 1 to 10:
x<-c(1:10)
Is any of the numbers 5?
any(x==5) TRUE
Are all the numbers 5?
all(x==5) FALSE
subset(), which(), and ifelse()
The functions subset(), which(), and ifelse() are probably among the most commonly used functions in R.
One way to filter the elements of a vector is to use the subset() function.
Let's first create the vector x.
x <- c(5, 4:8, 12) x 5 4 5 6 7 8 12
Now we want to keep only the elements which are smaller than 6.
y <- subset(x, x < 6) y 5 4 5
Now let's use the function which() to identify a position in the vector.
which(x > 8) 7
The function ifelse takes two statements to execute. If the condition is TRUE, the first statement is executed; if the condition is FALSE, the second statement is executed.
For example, if x < 6 return 1, otherwise return 0.
ifelse(x < 6, 1, 0) 1 1 1 0 0 0 0
You can also do something like this, which keeps the original value when the condition is not met:
ifelse(x < 6, 1, x) 1 1 1 6 7 8 12
Accessing rows, columns or elements in a vector and a matrix
When you work with vectors, you might want to select an element or a set of elements within the vector. For example, suppose you want to show the elements in the 2nd and 5th positions 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)
Select the elements in the 2nd and 5th positions:
x[c(2,5)] 5 3
Select the elements from the 2nd position up to the 5th:
x[c(2:5)] 5 7 2 3
To access rows and columns in a matrix, use the syntax x[row,column]. In the example below, I will create a matrix by using the function rnorm(), which generates random numbers from a normal distribution.
Let's create a matrix with 12 random numbers arranged 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 the 3rd row and 2nd column.
x[3,2] 0.208591
You can also show an entire column or row. Using the matrix from the example above, we now select the entire second column.
x[,2] 0.1125335 -0.8137567 0.2085910 -0.1885532
For a row, we use the code below:
x[4,] -0.1511682 -0.1885532 0.6812309
Another useful function is dim(), which returns the number of rows and columns of a matrix.
dim(x) 4 3
Adding and deleting elements from a list
Adding elements to a list is similar to adding variables to a data frame. Here we will show how to add elements to a list. The elements can be numbers or characters, and you can add more than one element to the list.
First, let's create a sample list:
y <- list(job="researcher", age="55") y $job "researcher" $age "55"
Now let's add another element to the current list:
y$name <- "Andre" y $job "researcher" $age "55" $name "Andre"
Let's see now how you can delete an element from the list by assigning it NULL.
y$age <- NULL y $job "researcher"
If you have any questions related to vectors, please post a comment below.
Just a couple of comments/questions:
You keep using the double bracket to index your vector (e.g. x[[2:5]]). This is the notation to access elements in a single-element list, but for vectors it’s redundant. Is there any reason you don’t just write x[2:5]? Also, when you introduce the which() function you write 12 as the output of which(x>8), but as you said, which returns the position, so which(x>8) will evaluate to 7 and x[which(x>8)] (or more simply x[x>8]) will return 12.