The fact that this also happens to be the value of the second element of .month is irrelevant. Obviously, the new variable february.sales should only have one element and so when I print it out this new variable, the R output begins with a because 100 is the value of the first (and only) element of february.sales. For example, I could create a february.sales variable like this: february.sales <.month This behaviour makes more sense when you realise that we can use this trick to create new variables. So, when it outputs 100 what R is saying is that the first number that we just asked for is 100. When we typed in .month, we asked R to find exactly one thing, and that one thing happens to be the second element of our .month vector. This is because R is being extremely literal. But there’s a subtle detail to be aware of here: notice that R outputs 100, not 100. Yep, that’s the February sales all right. February is the second month of the year, so let’s try this: .month # 100 Suppose I want to pull out the February sales data only. At this point, you might have a sneaking suspicion that the answer has something to do with the and things that R has been printing out. To get back to the main story, let’s consider the problem of how to get information out of a vector. It might seem a bit odd to you that R does this, but in some ways it’s a kindness, especially when dealing with larger data sets! For the second row, R has printed out the 9th element of the vector through to the 12th one, and so it begins that row with a so that you can tell where it’s up to at a glance. For the first row, R has printed out the 1st element through to the 8th element, so it starts that row with a. The important point is that the first line has a in front of it, whereas the second line starts with. But that’s not the important thing to notice. If that were the case, you might have seen output that looks something like this: .month # 0 100 200 50 0 0 0 0 0 0 0 0īecause there wasn’t much room on the screen, R has printed out the results over two lines. This would have happened if the window (or the RStudio panel) that contains the R console is really, really narrow. If you’ve been following along, typing all the commands into R yourself, it’s possible that the output that you saw when we printed out the .month vector was slightly different to what I showed above. However, before I do so it’s worth taking a slight detour. Now that we’ve learned how to put information into a vector, the next thing to understand is how to pull that information back out again. To use the correct terminology here, we have a single variable here called .month: this variable is a vector that consists of 12 elements. To do so, all we have to do is type all the numbers you want to store in a comma separated list, like this: 35 .month < c(0, 100, 200, 50, 0, 0, 0, 0, 0, 0, 0, 0) cat Function Statistics Globe 18.7K subscribers Subscribe 6.5K views 3 years ago How to remove everything in the RStudio. The simplest way to do this in R is to use the combine function, c(). Clear R and RStudio Console (2 Examples) Remove with Shortcut vs. The first number stored should be 0 since I had no sales in January, the second should be 100, and so on. The book is aimed at R developers and analysts who wish to do R statistical development while taking advantage of RStudio functionality to ease their development efforts. What I would like to do is have a variable – let’s call it .month – that stores all this sales data. A practical tutorial covering how to leverage RStudio functionality to effectively perform R Development, analysis, and reporting with RStudio. Let’s suppose that I have 100 sales in February, 200 sales in March and 50 sales in April, and no other sales for the rest of the year. Since my class start in late February, we might expect most of the sales to occur towards the start of the year. Suppose the textbook company (if I actually had one, that is) sends me sales data on a monthly basis. Let’s stick to my silly “get rich quick by textbook writing” example. In R, the name for a variable that can store multiple values is a vector. In this section, we’ll extend this idea and look at how to store multiple numbers within the one variable. When I introduced variables in Section3.4 I showed you how we can use variables to store a single number. It can be used to load packagaes (i.e. using a random quote from a package or from an API: setHook("rstudio.\)Īt this point we’ve covered functions in enough detail to get us safely through the next couple of chapters (with one small exception: see Section 4.11, so let’s return to our discussion of variables. Rprofile is an empty file that can be used to automatically modify your blank new environement every time you start a R session.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |