 To continue our introduction and accessing data, we want to talk about factors. And depending on the kind of work that you do, this may be a really important topic. Factors have to do with categories and names of those categories. Specifically, a factor is an attribute of a vector that specifies the possible values and their order. It's going to be a lot easier to see if we just try it in our and let me demonstrate some of the variations. Let's open up the script and we can run through it together. What we're going to do here is create a bunch of artificial data, and then we're going to see how it works. First one I'm going to do is I'm going to create a variable x one with the numbers one through three. And by putting it in parentheses here, it'll both store it in the environment and it will display it in the console. So there we have three numbers one, two and three. I'm going to create another variable y that's the numbers one through nine. So there that is. Now what I want to do is I want to combine these two. And I'm going to use the C bind or column bind data frame. So it's going to put them together, and it's going to make them a data frame. And it's going to save them into a new object I'm creating called df for data frame one. And we'll get to see the results of that. Let me zoom in on it a little bit. And there you can see we have nine rows of data. We have one variable x one, that's from the one that I created. And then we have y. And then we have the nine indexes or the row IDs there down the side. Please note that the first one x one only had three values. And so what it did is it repeated it. So you see it happening three different times 123123. And what we want to find out is now what kind of variable is x one in this data frame? Well, it's an integer. And we want to get the structure. It shows that it's still an integer if we're looking at this line right here. Okay, but we can change it to a factor by using as dot factor. And it's going to react differently then. So I'm going to create a new one called x two that again is just the numbers 123. But now I'm telling R that those specifically represent factors, they'll create a new data frame using this x two that I saved as a factor and the one through nine that we had in y. Now, at this point, it looks the same. But if we come back to where we were, and we get the type of it's still an integer, that's fine. But if we get the structure of DF two, now it tells us that x two instead of being integer is a factor with three levels. And it gives us the three levels in quotes one, two and three, and then it lists the data. Now, if we want to take an existing variable, and define it as a factor, we can do that too. Here, I'll create yet another variable with three values in it. And then we'll bind it to y in a data frame. And then I'm going to use this one factor right here. And I'm going to tell it to reclassify this variable x three as a factor and feed it into the same place. And that these are the levels of the factor. And because I put in parentheses, it will show it to us in the console. And there we have it. Let's get the type. It's an integer, but the structure shows it again as a factor. So that's one where we could take an existing variable and turn it into a factor. If you want to do labels, we can do it this way, we'll do x four again, that's the one through three, and we'll bind it to nine to make a data frame. And here, I'm going to take the existing variable, df four, and then the variable is x four. I'm going to tell it the labels, and then I'm going to give them text labels, I'm going to say that they're Mac OS, Windows and Linux three operating systems. And please note, I need to put those in the same order that I want them to line up to those numbers. So one will be Mac OS, two will be Windows and three will be Linux. I run that through, we can pull it up here. And now you can see how it goes through and it changes that factor to the text variables, even though I entered it numerically. If I want the type of to see what it is, it still calls it integer, even though it's showing me words. And the structure, this is an important one, let's zoom in on that just for a second. The structure here at the bottom is it says it's a factor with three levels. And it starts giving me the labels. But then it shows us that those are actually numbers one, two and three underneath. If you're used to working with a program like SPSS where you can have values, and then you can have value labels on top of them. It's the same kind of concept here. Then I want to show you how we can switch the order of things. And this gets a little confusing. So try it a couple of times and see if you can follow the logic here. We'll create another variable x five, that's just the one, two and three. We'll bind it to y and there's our data frame just like we've had in the other examples. Now what I'm going to do is I'm going to take that new variable x five in the data frame five df five. And notice here I'm listing the levels but I'm listing them in a different order I'm changing the order that I put them in there. And then I'm lining up these labels. When I run that through, now you can see the labels here, maybe yes, no, baby, yes, no, it's showing us the nine values. And then this is an interesting one, because they're ordered, it puts them with the less than sign at each point to indicate which one comes first, which one comes later. We can take a look at the actual data frame that I made. I'll zoom in on that. And you can see we know that the first ones a one because when I created this, it was 123. And so the maybe is a one you see, because it's the second thing here in each one. So one equals maybe. But by putting it in this order, it falls in the middle of this one. There may be situations in which you want to do that. I just want you to know that you have this flexibility in creating your factor labels in R. And finally, we can check the type of that. And it's still an integer because it's still coded numerically underneath, but we can get the structure and see how that works. And so factors give you the opportunity to assign labels to your variables and then use them as factors in various analyses. If you do experimental research, and this sort of thing becomes really important. And so this gives you an additional possibility for your analysis in R as you define your numerical variables as factors for use in your own analyses.