![]() 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) The simplest way to do this in R is to use the combine function, c(). The first number stored should be 0 since I had no sales in January, the second should be 100, and so on. What I would like to do is have a variable – let’s call it .month – that stores all this sales data. 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. \)Ī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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |