First Coding Steps | Creating and Working with Objects | R-Tutorials.com

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now it is time to finally start coding generally we code here in the script section because here tools like code check and auto completion are implemented by our studio of course you could do it the old-fashioned way and insert the code directly in the console below here but that does not really make sense I have a new script called basics which contains some commands I want to go through generally you could use our as a trend calculator if we simply type for example 4 plus 4 plus 5 and we're going to send it via run we get the correct output down here which is of course 13 note that whenever you want to run code the mouse needs to be in that specific line if you want to run several lines like in a function you can either mark the whole block of code and run it or you can put the mouse in the first line of the block and you can run each line one by one right till the end note that the space between the numbers does not matter or is not sensitive to that if I add several spaces over here I would get the exact same result generally we have the basic arithmetic operators like plus minus divided the star four times and the caret for power let's type a command with a specific order of operation let's say we want the third power of five minus three with the standard round brackets we tell our to it first calculate this part and the caret indicates the power of three of this calculation this would mean 5 minus 3 which is 2 and then subsequently you would be multiplied 3 times by itself which is 8 next we're going to create objects this is very important to understand because nearly anything you do in R will be done via different objects an object is basically an elias for data you want to bind together an object can be a whole data set you load into R it can be a result of the calculation or it can be a part of a data set with specific traits it is also important to note that an object can have different properties therefore we have different object classes in are those of you new to programming will hear this for the first time but it is crucial to understand the object class you select for your data will determine what you can do and cannot do with your data set the classic object class in R is the data frame it is similar to an excel sheet with columns for variables and rows through observations most of the functions in our work with data frames therefore it is the single most important object class for you to understand in this example we will now start with a very simple object probably the simplest object class in our the vector a vector is simply a collection of values of the same class an example would be stock price data for a company if you store the daily average price of Amazon stocks over 20 business days and you put them in one excel column you have a vector if you add the date as a separate dimension you would get a time series class or TS in R the price in this case will be a number with decimals therefore it is a numeric vector and not in integer vector all right so let's create our first object we're going to call it X XS 3 values here therefore it has the length 3 those are just the numbers 4 5 & 6 if we run this example we add the object to our environment here we learn that this is a numeric vector and indeed it is also confirmed that it has three values in it you assign values to an object by either using an arrow like you do here or you can use the equal sign the arrow can point to both directions as long as the object name is directed towards you are fine as you can see with the object Y it works just fine at this point you might ask yourself what is lowercase C means at the beginning of the values well this is an extremely important feature which stands for concatenate it tells our that all the values within the round brackets belong together they are one vector this little C can even be used within functions when you want to provide several inputs for one argument those arrows are the preferred way for assignments you can do the exact same thing in a more complicated way you can use the assign function if you are using this function you at first need to state the name of the object this needs to be done under quotations since we are dealing with a character and after the comma we can indicate the values we want to put in our vector here we need the concatenate C within a function we are telling the function assign that those three values in brackets belong together and should be used as one argument after we run this line we can confirm that it worked we have a new output in the environment over here which exactly matches our input we have the object name the class and even the values displayed here so this is now our new or updated object X as you might have noted down here in the console there is not much action going on at least not visibly we can see that there was a calculation sent to the console but we do not get the output this is because in the line of our code we only tell our that we want a new object called X in this case but we do not tell our that we also want to directly see this output to do this we have to type the object name in a new line and of course run it this is quite important to realize because when you run code the calculations might work just fine but you do not get to see the results without typing the name of the object by the way you can use the same a column in state the name of the object once more like you do here let's say McCullen is like putting code in a new line and you will get the output right away just by executing this one line of code by the way and this is extremely important our is case-sensitive if you name a vector lowercase Y you need to call lowercase Y in order to print it I would not recognize uppercase Y in this case so you would get an error message managing your objects is not only possible in the environment you can of course also code this for example if we want to see which objects we already have created in our session we can run LS on empty brackets or objects on empty brackets this basically gives you a list of occupied object names if you will so in this case we already have the objects x and y created in this our instance of course if you assign in you vector to the name X like we did several times in this session the old vector X will be overwritten and the data is lost to remove an object from the environment we can run RM and then the name of this object now we just delete it X from our environment note that whenever you want to refer to a name you have to use those quotes they tell our that this should be considered a character so now that we have all those vectors created and we know how to manage our objects let's get a step further let's work with those objects we know that we have the object Y already created which is this vector over here in this example we now want to create the object X which should be a vector of two times y and a 5 in the middle the whole thing should have doubled the length of y plus an extra digit let's run the next line and let's also see what we would get in this output and indeed we have a repetition of why with a five in the middle so that is how you can use an object within an object and that also explains quite well the power of those objects just by typing why you have incorporated three values we can now use all sorts of functions on our new object X for example we can sum up all the values the command for that would be sum and then in round brackets X so this would be one hundred and seventy five point four in this case again it is really easy to do those kinds of calculations when you have all your data packed in this single letter X it also leaves nearly no space for typos alternatively we can also get the square root of x in this case we get another vector of the same length for each single value of the vector we now get the square root by the way when you have a long vector and you want to see which value hides on position say one you have to use the Box brackets box brackets always indicate the index position in this case it would be the number which is the first value of the vector X as we can also see here so the output is the expected value of four in the next example we will perform a test on our vector X we want to know if the values are smaller than five or not that means we would expect a vector of the same length of X with the boolean values true or false since a logical test can only be true or false this time we will store the results directly in a new object which we will call new object I will use the same a colon trick here to get the output right away in the same line as you can see the positions one and five that are smaller than five they are denoted as true the other do not fulfill that criteria therefore we have a force we can double check the results with the original vector X and this indeed confirms that those positions 1 and 5 are smaller than 5 now I want to explain to you when to use which types of brackets basically we have three types in our round box and curled brackets the round brackets are this standard form you will most of the time use this type of brackets whenever you want to indicate that the given portion of values belong together you have to use this type of brackets also the predefined functions in our use those types of brackets the Box brackets on the other hand are mainly used for data filtering purposes they indicate the next position within an object depending on the object class you can specify the simple index position in a vector which has only one dimension you can specify row and column index in a data frame to extract a value from a data frame generally for each dimension of your object you have opportunity to specify an index ID and at last we also have the curled brackets which are mainly used when you write your own functions the second part of a self coded function needs to be specified in those curly brackets you
Info
Channel: R Tutorials
Views: 3,169
Rating: 4.8888888 out of 5
Keywords: learn r, r tutorial, r coding, r programming, r objects, r beginner
Id: CogyulOswR0
Channel Id: undefined
Length: 14min 39sec (879 seconds)
Published: Sat Aug 13 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.