R Data Structures 1: Vectors
Creating a Vector, Vector Classes, Vector Indexing, Vector Sequencing, Vectors are Mutable, Vector Subsets, and Vector Subsets + Data Manipulation in this post.
This is the R counterpart to Python’s data structure post located here.
Table of Contents
Creating a Vector
Vector Classes
Vector Indexing
Vector Sequencing
Vectors are Mutable
Vector Subsets
Vector Subsets + Data Manipulation
1 - Creating a Vector
A vector is a one dimensional array. A vector can be creating using any basic data types such as: numeric, character, integer, etc… The vector should have elements of the exact same type of data. In order to create a vector we will use the c( ) function which is short for combine. Here is the documentation for it:
Now, let’s use R to actually make a simple vector of 3 elements.
The above code creates a simple vector called Python_Sux, which as 3 elements in there: ‘Celt’, ‘Devil’, and ‘Hal’.
Here is a pretty decent video on the different types of data you can have in vectors:
2 - Vector Classes
When working with vectors, it is sometimes important to know what kind of elements you have within your vector. The class of a vector is the class of the elements of a vector. For example, in the above vector, the classes of the 3 elements are a character, this means the class of our vector is also a character. In order to see the class of an element, we will use the class( ) function. The documentation for this is below:
Here is a snippet of applying the above class( ) function to our vector.
3 - Vector Indexing
In R, we can access the elements of a vector using their indexes. The first element of a vector is kept at index 1, while the last element of the vector is kept at index n, where n is the total number of elements in our vector. In this example, we’ll make a vector of numeric values, and grab the 2nd, and the 4th elements.
Hint: You can use the length( ) function to get the number of elements in your vector. Super useful.
In the above example, we used another vector to tell R where to place the actual indices, I’d highly recommend getting used to doing indexing in R, because this is one of the fastest ways to wrangle data, instead of relying on some functions.
4 - Vector Sequencing
Something that is highly useful is to quickly generate a vector from a certain number, that goes up/down to another number, that has each step go up/down by another number. If you’ve done python before, I’m basically talking about the R version of Python’s range( ) function. In R, we go with seq( ) for this. Here is the documentation:
In this code snippet, we’ll use the seq( ) function in order to make a sequence of numbers from 1 to 100, going up by +7 each step.
5 - Vectors are Mutable
Mutable data types can be changed after they are created. In most cases, this means assigning a new value to a variable. For example, you could reassign the variable "age" from 25 to 26 and the new assignment would store the value 26 in that variable. You can also mutate data structures like lists and dictionaries by adding, removing, or replacing elements.
Mutable objects are usually called "variables" while immutable objects are usually called "constants". However, technically speaking, both variables and constants can be either mutable or immutable depending on how they're defined. For example, in some languages like Java, all objects are considered mutable by default unless marked as final (which makes them immutable).
Vectors in R are mutable, once created, you can change the value in them. In this example, we’ll using indexing in order to pick the 2nd element in our vector, then change it’s value.
6 - Vector Subsets
If you would like to subset your vector, you can just put the condition that you are looking for in the indexing section. That is just put the condition inside the [ ] brackets. In this example, we’ll make a sequence from 1 to 10, then use subset in order to keep only elements less than 5.
7 - Vector Subsets + Data Manipulation
In R, you can actually combine vector subsets, and data manipulations in one quick move. The basic idea is to use the vector subsets from up above, and then from the subset you have, you simply assign that group another number (due to mutability), and voila it works. In this example, we’ll take a sequence from 1 to 10, focus on those less than 5, and assign them the value of 10.
Hopefully this section focused on R shows you why even though R is basically like “an obsolete prehistoric fossil” it is still being used today. Will have plenty more stuff for those of you guys who focus on R, and on some of the data wrangling techniques easily accessible in R.