If you haven’t already, I encourage you to read the first article in my series to gain a basic foundation of R and R Studio. You will also find the links for downloading the programs there as well.
To begin, I want to mention a bit about the “packages” found in R. The incredible thing about R is that is a dynamically evolving language that gains functionality on a daily basis. Packages allow for anyone to compile functions and data sets together in one convenient bundle to extend to the base system functionality of R. There are two main repositories that host packages, CRAN and bioconductor. At the time of writing this article there are more than 9,000 individual packages available for use with R.
A majority of the packages will be installed from CRAN, so I will highlight the steps to install and load packages hosted there. The first thing you will need to do is to install the package, as an example we will install the ggplot2 (one of R’s most popular graphing packages). Once you have the package installed, you must then load it. The code is as follows:
Alternatively, utilizing R Studio, you can click the Packages tab in the bottom right window, click install, and enter ggplot2 in the Packages field (seen below):
We will be using multiple different packages in this series but I will notify you which ones you need before I expect you to use them, just always remember to load the packages after you install it!
Concept 1: Objects
Now I would like to introduce you to a few of the functions and capabilities of R. Starting from where I left off in Article 1, we were observing that R can be used as a calculator:
Any time there is output in R, the result is assigned to a hidden named object .Last.value. Objects are one of the biggest foundations to R’s amazing functionality. Let’s try calling the object “Last value”
As you can see, the output is the output from our last line of executed code. This can come in handy later when you are performing multiple manipulations on different values. Now, lets create out own object.
The assignment operator (<-, you can also use “=”) dictates that the proceeding text gets assigned the information that follows the operator. I have also included the use of couple functions that return properties of the object.
Concept 2: Functions
Speaking of functions, lets take a look at some of the different built in functions of R. You could call simple math functions:
Notice all of these functions return the same value. There are multiple ways of manipulating data how best suits what you need with R. However, take note:
Now returns sqrt(2) because the last value outputted was 2, not 4 anymore. Keep that in mind if using a dynamic object like .Last.value. The cool thing is, you can even perform mathematics on objects!
Vectors in R are single entities consisting of an ordered collection of items. To construct a vector you, the most common thing to do is to use the concatenate function c( ). To do so, seperate every new element in the vector by a comma. For example, you can have numeric vectors:
The first line of code only defines the vector, the second will allow you to see the output in R’s console window.
This output brings up an interesting point I want to make. The console window will output vectors, and other things like matrices and data sets, item by item. The []1] on the left shows the position of the current element you are looking at. So if for example, you have a vector of 100 elements, R will indicate to you which element starts the new row by a bracketed number (see the screenshot below for an example).
You can make vectors full of items that are other functions:
Other ways of creating vectors includes using the sequence and repetition functions [seq() & rep()]. You just have to remember to pass the arguments for by what interval the sequence forms or how many times to repeat the item.
Perhaps one of my personal favorite things to do in R is matrix manipulation. Instead of having to put all my information into a matrix in my calculator I would rather use a program on the computer, like R, to carry out such calculations with ease. And the wonderful thing is that R has some incredible capabilities.
Notice to populate the matrix I chose a sequence of number between 1 and 10, that is the 1:10 operation, and defined the number of columns I wanted, ncol=2. The output is displayed in a way that makes sense:
Where the bracketed numbers on the left show the row position and the bracketed numbers on the top show the column position. Combined, this means that []3,2] is the element in row 3 column 2, which in our case is 8. We could also use the power of R to have it output this answer to us:
Pretty trivial now, but could come in handy when you have large matrices and you need to perform operations on a specific element, row, or column.
It is important to mention there are some very handy arguments the matrix function can take, I will show you them in the following steps but for future reference you can call the help() function to see the types of arguments functions can take, for example if we need help with the matrix function:
We can perform some operations with this matrix such as the transpose, adding a constant to the matrix, multiplying by a constant, adding two matrices, multiplying by a vector or another matrix (listed respective order below):
I encourage you to explore some of these different operations and functionalities yourself. There are so many things I haven’t touch upon that utilize these basic concepts but if you have any questions about what I have mentioned or how to go more in-depth, please leave a comment.
Also I would like to mention if there are any functionalities you would like to see in my upcoming articles, please let me know!
Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
Nice article, Matthew.
I'm also a R enthusiastic and a self learner.
Just want to call your attention for the link to the next article. You didn't want to add it here in this article or it was on purpose?
Have a question about something in this article?
You can receive help directly from the article author.
Sign up for a free trial to get started.
Comments (2)
Commented:
Commented:
I'm also a R enthusiastic and a self learner.
Just want to call your attention for the link to the next article. You didn't want to add it here in this article or it was on purpose?