Fundamentals of R
The course is part of this learning path
This module introduces you to the some of the basic data structures that can be used in R
The objectives of this module are to provide you with an understanding of:
- What a vector is in R
- How to create a sequence
- How to create a vector using a repetition
- How to pull elements out of vectors
- Vectorised operations
- Logical comparisons
- Strings in R
- Undefined situations in mathematics
- 0, NA, NaN, & Null
Aimed at all who wish to learn the R programming language.
No prior knowledge of R is assumed
Delegates should already be familiar with basic programming concepts such as variables, scope and functions
Experience of another scripting language such as Python or Perl would be an advantage
Having an understanding of mathematical concepts will be beneficial
We welcome all feedback and suggestions - please contact us at email@example.com to let us know what you think.
- [Instructor] When trying to enter strings into R we can enter strings using the double quotation marks, we can use single quotation marks, we can input in numbers as strings, we can use capitalized characters. The preferred convention is to use double quotes. Before thinking about how to print out onto a screen or an output device, I'd like to just cover off some special characters that exist in R, which are formally known as C-style escape sequences. For example we have, if we wanted to input in a new line as part of our output, we would use the escape sequence n or \n, we've also got the tab available. If you wanted to see a list of potential special characters, you can type in question mark quotes, which is part of the base, it will take you into the help window, it's part of the base R package and inside of here we can see a list of various different special characters that you can use. For example, if I had a string such as happy\nsad, where I wanted to see every character as I had, as a raw string printed out onto an output device, I can use the print function. But if I wanted to maybe utilize the escape sequence n for the new line, I could use the writeLines sequence, quote function, with a capital L to convert the \n into the output to have a new line. As an aside, if I wanted to have a quotation mark, inside of my sentence, I could use double quotes to get the entire sentence included and use single quotes for my exclamation marks. Here I'll show you how to use the tab and just explain that \t represents white space. Let's say I wanted to have the \t as part of my output when using writeLines, I could use the double quotation, the double slash mark here. Okay. How can I concatenate characters into vectors, of vectors? So let's say I had the number two and I wanted two of those, I can double the number. I can't repeat the same thing with text. This will return an error. Let's say I had, my name is defined in the name variable N A M E, and I'd like to add two pieces of strings together, so some languages allow you to use the addition operator on strings or characters. Unfortunately we can't do that here either. So in R there are three different types of ways to combine characters together. I'll show you the combine function first. So we can combine three strings, football, cinema, dancing, into hobbies using the C-function and I can return the out onto the screen as such. I can show you the cat function, which is short for concatenate and if I wanted to have a list or have a group of names together, I can do that. For example, using a variable and also a string. And how do I know what is the separator? Well, it's a space at the moment, that's the default separator. But if I wanted to change the separator to being anything else I could, for example, if I wanted to have no spaces, I can. I did that by deleting the gap here. The third function you can use is paste and this is useful where you may have a, for example, a prefix and you'd like to say, given this error has occurred, we have a user missing. You could save that as a message and then return that message as part of your functionality of using a piece of error code. Now if I was to say, can I change that separator, I can, I can use a sep argument, for, and amend it to colon, space, to make it just more user friendly and a bit cleaner on the output. I'll just show you at the bottom of the screen that we can use vectors of different lengths. Here I have a vector of length two and I am connecting that, pasting that together with a vector of length 10. What happens is that the short vector is recycled five times to mirror the length, or whatever is required, of the longer vector. Finally, if you have a string or a character vector and you'd like to know how many characters exist in this, you might naively try the length function and that will only turn and tell you that you have one piece of information in this variable. I could try it as a vector, and again, I'd only receive back one. Just to prove the point, I will try that with two strings and it will return back the number two. There is a function that we can use called nchar, for the number of characters, and in this case here I can have a vector of strings and I can return using the vectorized version of nchar, five the first string, five for the second string, and seven for the academy at the end.
About the Author
Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.
Kunal has helped clients with early stage engagement and formed multi week training programme curriculum.
Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation.
Data Scientist at a credit management company; applied statistical analysis to distressed portfolios.
Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform.
Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data.
Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.