Transcript for:
Beginner's Guide to R Programming

welcome to r programming 101 if this is the first time you've looked into using r so r is a programming language we use it for statistical analysis and data analysis and this video is specifically for people that have literally never looked at r before i'm going to walk you through some of the basics at the end of this video you're going to really feel comfortable with respect to what you can do with r and how to use it and how the kind of thinking works how does how do people use r to do data analysis i'm going to keep it nice and simple i'm also going to show you a few tips and tricks and you're going to feel as if there are things that you can do with your data in our right from the word go okay so i'm going to keep it simple you're going to learn something stick with me you're going to love this and i love r and at the end of it i think you're gonna kind of think to yourself that you love r2 okay so stick with me if you want to learn about our programming then you have come to the right place on this youtube channel we're creating our programming videos on everything [Music] let's look let's look at the console okay what we've got here is we've got up at the top on the left right over here we've got what we call the source this is where we write our code and i've written some code just to illustrate how r works the first line of code is i've typed in five plus six and if i push ctrl enter we see down here in the console at the bottom we see it it's performed that like it would have calculated ignore the one in brackets that's just telling us how many lines of answer is going to pop up on the screen so ignore that here's your answer 11 5 6 is 11. that's the first lesson r can be used it is basically a very sophisticated calculator if you think about it like that okay now let's take this a step further we can say we can assign values so if we just type in the letter a we could type anything we wanted there and we use this little arrow which is which is the greater than and the little dash making it looks like an arrow so we're saying take this value five and assign it to that letter a enough push ctrl enter you will see the little value a with with the value next to it right so that's the next lesson we can and we can assign anything to anything right it doesn't have to be just just a number that we can assign all sorts of complicated things so that you know you'll get used to what i mean by that but the principle here is you can assign one thing to another fair enough and we assign 6 to b and then of course now we can say a plus b and once again of course we get 11. the next lesson is there are functions in other words there's a whole lot of pre-built machinery that you can use and the simplest one just to illustrate the point is sum right so we've got sum and whenever you've got a function you open and close the brackets and inside the brackets you put in what we call the arguments right so what is going what is this function going to be applied to well in this case we want to apply the function sum to a and b which we know we've assigned these values to and of course if we push control enter voila as you would expect it says 11. right simple are you confused yet no of course you're not because it is that simple okay let's keep going next we can take here we i'm creating a variable but i want more than just one value assigned to that variable i want there to be perhaps a string of values and in this case i'm calling it this is a variable and i'm going to i'm calling it name and assigned to name i've got two data points greg and jill and this little c here this the technical term is it stands for concatenation really think of it as that's here saying name is going to be a a combination or a compilation of everything inside the brackets separated by a comma and if i push ctrl enter voila we've got name it it knows that this is a character variable and then it it'll tell you what they are right over there so in fact the video you're watching my the video of my face is in the way but behind behind this this picture of me the first few data points in that variable right and then of course if we control enter on name it will generate it'll print them out down here we can see greg and joel right there okay so first lesson is it's a big calculator you can assign one thing to the next and you can assign multiple data points to a single entity and that becomes a variable now this is where things get exciting i've done i've created a bit of code here and i'll walk you through it super duper simple you're not confused yet of course you're not because this is that simple right here's a couple of variables i've made we've said name and i've assigned a three names to that i've said age and i've assigned three ages just numbers and you'll notice because these are numbers they're not needing the inverted commas because it's not a word or a string r can pick up that that's a number and then gender i've just said m and f for male and female right so we've got name age and gender and you can see in the environment over here we can see age gender and name have popped up here and we can see that they now exist in our environment these are objects that we've got that we can work with data objects of some description so we've got these three variables and we can put them together right in a data frame and we can give that dot frame a name and that becomes another object that we can work with and i've called the starter frame friends and i use the function data frame and inside the brackets i put the arguments of that function and in this case these three objects name age and gender and if i push come on control enter voila in our environment on the right hand side you can see right here a new object has been created this is a dart frame and if i click on it we can see here is our little data frame three variables name age and gender and we've got three rows or sometimes we call them observations greg is 47 and it's mail et cetera all right this is a nice little simple data frame a super quick interruption to this video to say thanks to nested knowledge nested knowledge sponsored this channel and i absolutely love them nasa knowledge is an online platform that you can use to do literature review and systematic lit review and what i love about this platform is that i'm using it for the entire process beginning to end all the way from search screening tagging extraction all the way through to actually writing the manuscript creating a living document online i've got my entire team using it so we collaborate different people doing different parts of the process i used to hate litterview now i love it if you want to love lit review check out nested knowledge click on the link in the description below and without further ado on with the video okay so now we're cooking with gas we're working in our let's go back to our this is this is a little bit of code that we've written now in r if you want r to look at and do something with one variable in a data frame so we've got three variables now we've got name age and gender but we want to do something just with name or just with age or just with gender and we want to tell r how to look specifically at that variable we use this little dollar sign right so if we type in friends that's the data frame dollar which means that within that data frame we want you to look at this variable name which is the variable that we've created if i push ctrl enter you'll see down here it prints out all of the items or the data points within that variable okay that's interesting what if we wanted to subset we wanted to extract out a part of the start frame now i'm going to show you something and it might seem a little bit complicated but don't worry because within a few seconds i'm going to show you a much much easier way to do the same thing now you have to understand the basics in other words i've got to walk you through this kind of subsetting using the square brackets don't panic don't feel anxious in literally 30 seconds i'm going to make this so simple it's going to be super duper easy so stick with me okay if i take the data frame friends and i want to take out a certain row or a certain variable or certain combination of rows and variables right how do i tell our what part of that data frame to look at well we can have these two square brackets next to it with a comma in the middle right and in the space before the comma i tell r which rows i want to extract i want to include in this opportunity to look at the start frame so before the comma are the rows or the observations and after the comma it's the variables right you got it okay so and if i left that completely blank if i left nothing in either of those spaces and i just push ctrl enter it would assume that i want everything right so leaving it blank means it gives me everything down here okay if i put something in here if i put a number one it's going to say okay we want the first row and all of the columns because we've left the columns blank so ctrl enter there we go just the first row pops up in our console down there if i put a number one after the comma that means you know telling r what what columns i want to look at control enter it just it's just taken out one data point because that is the the first row and the first column there's just one cell one data point and that's just my name greg right there okay i could put one two three and then it would control enter and then it's selected all three it's selected the column it's selected is the first column so it's the name column and it i've asked for all three observations and that's what's happened right there and i don't want to waste too much time here but i could ask just for the first column but all three of the first row but all three columns and that's what it's done right there okay so that's how we can kind of subset and ask for bits and pieces of the data frame we can add to that we can say friends in the columns that we want to select or subset if we say friends dollar sign age so with respect to the age variable if it's less than 50 select those and then select these columns of the first and second column right that seems a little bit complicated and i know this looks a bit messy and you feel as if oh my goodness now we're getting into this programming language and i'm already confused don't be because in a few seconds i'm going to show you a much easier way to do this i just want you to understand how it is that we do this so let's push ctrl enter and that's what's happened here it's looked for any rows where the age is less than 50 so there's just two of those and it's taken out just the column for name and age so we've subseted our data set quite neatly there but i agree looking at that it does look a little confusing so let's look at a much easier way to do exactly the same thing okay nr this is the next lesson i want to teach you and you're not going to get too much into this yet but i want you to have hope see the future this is where you're going in our there are lovely packages that you can install that help you that give you expand your vocabulary that make r easier and easier to use and the best one if you say if you've installed the tidy verse and incidentally you need to at some point install the tidy verse and it's as simple as saying it really as simple as typing in install packages and then you would you would say inverted commas tiny varies like that and you would push ant and it would install the tidy verse right it would put it onto your computer and you'd never have to do that again you only have to do that once it's then it's there and you've got it right but then every time you want to use it at the beginning of your programming session you would say library which means use this package and the tidyverse is actually a whole collection of packages it's great and we push ctrl enter and it's r is going to say okay i'm going to go and fetch the tidy verse and get all of that additional vocabulary and use that now don't worry you don't have to be able to use these packages yet i just want to show you where this is going so that you feel as if using r isn't complicated it is easy i can do this right and you're going to see why it's so easy right now when you're working within the tidy verse it's a whole different ball game now we're starting with saying let's start with with the data frame friends and this little thing it's called a pipe operator it looks confusing but believe me it's your best friend it's basically percent greater than percent think of that as just saying and then right because it pipes in whatever's from the left-hand side of the pipe operator into what's ever next to it on the right-hand hand side and by the right hand side i also mean underneath that's fine all right so we say friends and then select because we want to select just two of the rows name and age and then filter by age is less than 50. and if we stop there okay and push ctrl enter voila we get exactly the same as what we got this here we got by doing friends and open square brackets complicated right by doing the exact same things with just this very easy to understand language this very natural language of select these columns and and filter by this criteria will end up with the exact same little extract the exact same subset so it's super easy really nice and then we can add other things to it like you know for example for example we can say and then arrange by age and you see originally we've got it's arranged you know the 47 is above the 34 but now if we if we run it it's doing it smallest to largest arranged in size order of age okay look i know we've covered quite a lot what i'm hoping is that you've seen first of all r is super easy to use it's not complicated right once you've got your head around these principles everything else is basically an expansion of what i've just shown you and the exciting thing is now that i've introduced you to the idea that there is the tidy verse and there are these packages and there is this expanded vocabulary that really uses a very natural language to do things now that you've seen that that is possible as you learn r at least you kind of know where you're going you can be like okay i have to learn some of the basics but i'm very quickly going to learn how to use these packages like for example the tidy boost and i'm going to really start getting into a version of r which is intuitive which uses a lot of natural language which is really not complicated at all you're going to love it i love it get into it thank you for watching the video don't ever change don't do drugs always do your best watch the next video take care speak to you soon bye