How to Adapt the addPercent Function in R
How to Add Color and a Key to Lattice Charts in R
How to Distinguish Data Types in R

How to Get Values out of Vectors in R

Vectors would be pretty impractical if you couldn’t look up and manipulate individual values. You can perform these tasks easily by using R’s advanced, powerful indexing system.

How R does indexing

Every time R shows you a vector, it displays a number such as [1] in front of the output. In this example, [1] tells you where the first position in your vector is.

This number is called the index of that value. If you make a longer vector — say, with the numbers from 1 to 30 — you see more indices. Consider this example:

> numbers <- 30:1
> numbers
 [1] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14
[18] 13 12 11 10 9 8 7 6 5 4 3 2 1

Here, you see that R counts 13 as the 18th value in the vector. At the beginning of every line, R tells you the index of the first value in that line.

If you try this example on your computer, you may see a different index at the beginning of the line, depending on the width of your console.

How to extract values from a vector in R

Those brackets ([]) illustrate another strong point of R. They represent a function that you can use to extract a value from that vector. You can get the fifth value of the preceding number vector like this:

> numbers[5]
[1] 26

Okay, this example isn’t too impressive, but the bracket function takes vectors as arguments. If you want to select more than one number, you can simply provide a vector of indices as an argument inside the brackets, like so:

> numbers[c(5,11,3)]
[1] 26 20 28

R returns a vector with the numbers in the order you asked for. So, you can use the indices to order the values the way you want.

You also can store the indices you want to retrieve in another vector and give that vector as an argument, as in the following example:

> indices <- c(5,11,3)
> numbers[indices]
[1] 26 20 28

You can use indices to drop values from a vector as well. If you want all the numbers except for the third value, you can do that with the following code:

> numbers[-3]
 [1] 30 29 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13
[18] 12 11 10 9 8 7 6 5 4 3 2 1

Here, too, you can use a complete vector of indices. If you want to expel the first 20 numbers, use this code:

> numbers[-(1:20)]
 [1] 10 9 8 7 6 5 4 3 2 1

Be careful to add parentheses around the sequence. If you don’t, R will interpret that as meaning the sequence from –1 to 20, which isn’t what you want here. If you try that code, you get the following error message:

> numbers[-1:20]
Error in numbers[-1:20] : only 0's may be mixed with negative subscripts

This message makes you wonder what the index 0 is. Well, it’s literally nothing. If it’s the only value in the index vector, you get an empty, or zero-length, vector back, whatever sign you give it; otherwise, it won’t have any effect.

You can’t mix positive and negative index values, so either select a number of values or drop them.

You can do a lot more with indices — they help you write concise and fast code.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Select Naming Styles in R
How to Work with Lookup Tables in R
How to Use Regular Expressions in R
How to Apply Functions on Rows and Columns in R
How to Provide the Right Information in an R Minimal Reproducible Example