In this tutorial, I will show you how to create tables in our so a table you can think of it as being broadly related to a data frame, so we think about the data frame as having data in rows and columns where typically the columns are going to be the variables where each column represents a unique variable and the rows represent unique cases or entities Now a table and you can think of actually more specifically that as a data frame as a specific type of table and the reason. I say that is that a table can have additional functions and properties that can be useful so tables are great ways to describe data and so in this tutorial, we’ll learn some very basic ways to create tables and specifically what might be classified as cross tabulations of two or three variables. And so the first thing that we need to do is make sure we have our studio opened, and I’m going to create a new file, go to file new file our script to create an R script file and specifically, this is a script editor. We can work from here and I’ll do file save as to make sure. I’m saving my work as I go along and I’ll call this our script file test. It already exists, that’s fine. And now the purpose of this is creating tables in our OK, So the first thing. I’m going to do is I want to set my working directory? I’ll move very quickly through this. If you have any question about a working directory is or how to read in data. Be sure to check out the tutorials on those topics. So I’m going to set the working directory as my H Drive Our workshop folder. It’s actually already set, but just for the sake of demonstration. I’ll run this. You could. Alternatively, go to session set working directory and choose working directory to do this manually. So our working directory is where we’re specifying our data live for this particular session here and specifically, we want to use this very our this. This data set or data file here. That’s a dot. Csv format. You can see comma separated values. Csv here and its called employee demographics, no space capital, e capital. D I’m gonna copy this, so I get exactly the right name when it comes to reading it in, so let’s read in the data and in this case. I don’t need to do this, but in case you need to do this. We’re gonna use the reader package to read in the data here. I’m not going to run this lot that line of code because I don’t. I’ve recently installed this, but I do need to access the reader package. And the reader package has the read underscore CSV function. We want to use so the the argument we’re going to enter The library function is reader, which will access this for us. So let’s hover our cursor on that line and click. Run, okay, now we’re ready to read in our data. So what do we want to call these this data file that we’re reading in? I’m gonna arbitrarily called as demo data not because it’s a demonstration because it’s demographic data as the name implies so demo data. All one word lowercase is what I’m naming it. You can call it whatever you’d like, and I’m gonna use that read, underscore CSV function here to read in exactly that name and remember. I copied this earlier so now. I’m pasting it in here. And so its employee demographics. CSV needs to be exactly written the way that it’s it’s appears in your working directory folder and make sure that you include that dot. Csv extension there now. I’m naming this data frame. I’m reading in as demo data. Okay, so let’s click run there. Alright, so we could click here and view it that way, but I’m gonna do view the old-fashioned way here down in my console. I’m just gonna type in view demo data. You could write this in your script editor too, but I don’t really want to save this part. I’m gonna hit enter here and this pops up this tab here where I can view the data so you can get a lay of the land. There’s some basic employee demographic data here that we can work with, OK, so we’re going to be creating some tables from the data that are available here. OK, so that brings us to the next part, which is let’s use the table function and do a two-way table. And when I say two-way, it means two variables in the table, and so there’s two different ways. We could do this so just starting off. You just use the table function, which is all lowercase from they saw here, and we need to specify the names of the variables and the data frames from which those variables belong. So let’s say that we want to do job level and sex. These are two of the variables, so its job level with the capital J and Capital L and sex with the capital S and we want to do a two way table, based on those well using the table function. We need to write this as following. We need to first put the name of the data frame to which each one of those variables belongs, so the first one is going to be job level. So that’s our first argument there, and we use that dollar sign to signify that this job level variable belongs to this demo data data frame object that we created. We read in and create it above. Now, let’s do the same thing for the second variable we wish to use in this table and that is going to be sex employees sex there, okay, so now we have two arguments here. First argument followed by a comma second argument here and let’s go ahead and run that and see what happens in our console. Okay, so what you can see here? Is that actually right here? These values 1 2 3 4 5 these correspond to the 5 different job levels here. OK, so you can see job levels here. They’re gonna range from 1 to 5 and then we have males and females here. So this would be 47 females and 35 males in job level 1 Okay, so this could be a way you could go through and examine and see proportionally or rather raw numbers wise. How many males versus females are at each level of the organization here we can already start to see what might be evidence of some type of glass ceiling effect within the company, where we see 8 males at the highest level and only one female and similar at the next highest level 7 males and 3 females. OK, so the this is one way we can create it, so if we just want to create a temporary table just when we’re if we’re examining our data, but what if we want to save this table as an object? Well, then we need to go ahead and name it, so I’m going to call this table 1 and use the left handed arrow to name, and then all I do is copy this and paste this here, so I’m just naming what I just did to create a table object and so let’s click run to run that, and let’s highlight this to just select this the name of this. This is the same thing as doing view and click run and we’ll see that. Yep, it’s the same thing, but now we’ve got an object here that we can work with, okay, and the reason that we’re gonna do. This is because now we want to do some proportions with this table, and so that the way that we can do that is by taking this object we created so this is doing proportions and the we’ll take that object and we’ll use it as an argument in the following function and specifically, that function is the prop the table function. Okay, and this stands for proportion table, so it’s prop table, and then we enter the name of this object that we created here as the sole argument. Okay, and so let’s click run. Okay, so now we get the proportions here, So it’s the proportions over all these are or overall proportions, not by row or not by column. I’ll show you how to do those Nicks. But so we can see that. About 29 percent, females are at level one and 20 or rather 29 percent of the total workforce happens to be females working at level One job level one. Okay, well, right off the bat. You can see that this is not so easy to read because of the number of digits that are reported after the decimal point. So how do we round well to round? We use the round function from base. R And so what we do with the round function is we take what we just this script? We just wrote copy and paste it within the function, so now we’re nesting a function here and so the what we just wrote will be the first argument, then well type in a comma, And then the second argument is going to be. How many digits do we want to round to? After the decimal lets? Just choose two here. So now let’s click run. This is a little bit better and easier to read here, okay, So it’s the same data but now just rounded to two digits and alternatively, we could do three digit click. Run there like that and you get three digits. Okay, so let’s turn it back to two digits there. Okay, so now let’s say that we are actually interested instead of looking over all proportions, we’d like to do row proportions. Okay, so what this means is we’ll get the proportion per row, so each row will add up to one or a hundred percent, depending on how you’re thinking about this. Okay, so to do that, let’s copy this line that we were just building up here and paste it below and, okay, so now we’re back within, so we’ve got nested functions here, but we’re looking at the prop table function and so currently we just have one argument for it. It’s the name of the table object that we created above. Well, now, let’s add a comma, so we can add a second argument and let’s add a 1 here and by adding a 1 This is designated that we want to reference rows, so we want row proportion specifically, so we keep everything else the same. We’re still rounding and everything like that, so let’s click. Run, okay, so now what you see in our output here in our console. Is that now, each row adds up to one or a hundred percent. If you think about this in terms of a percentage, in fact, if you want to think about this as a percentage, it’s super easy. Lets type in a hundred times because this is something you can do with the table object a hundred times what we just specified there in terms of our script, so this will multiply a hundred by all the values in here, which will convert the proportions to a percentage click run, and now we have it. A percentage format here, so 57 percent, 43 percent and so forth and so on now. How would you then do let’s copy this? How would you do column proportions? Well, it’s really simple. We’re just gonna change one thing and so. I’ve just copied what we did initially with this and so now we’ve got the nested functions where we’re rounding everything, but it’s we’re really interested in the prop table here and so again, the first argument is the name of the table object we created here and now comma second argument instead of one. If you put two, this is going to say now reference columns. So now let’s do column proportions. Keep everything else the same and run. And so now what you’ll see is that each column adds up to one and so here you can read so this would all the column marginals would add up to one there and we can do the same thing where we could multiply this whole thing by a hundred by doing one hundred and the asterisk symbol, followed by the what we’ve specified there and you convert it to a percentage. Okay, so what happens if we want to do a table function? But we want to do a three-way table, so with the three-way table, it’s really quite simple, so let’s just call this new one. We’ll create a new table called table two. Well name it that and then using the table function. Let’s borrow from this up here. The first two arguments let’s still use we’re still using that same data frames of demo data and job level and sex, but let’s add an additional argument here and do so demo data dollar sign. And then what do we want to do? Let’s do race ethnicity. How about so? I think that was race ethnicity. Yep, the capital, R and a capital E. And so now we can create this table here. Click run, okay and lets. Now view this table, so let’s just highlight just the name of that data at that table object we created. Okay, so now we get this displayed in the following way, we have separate tables, essentially since this is a three-way table. The default is to do it by levels of the third variable. You enter here, so we have. Asian employees table black employees, Hispanic, Latino, white. And then within this, we have each one of these. We have job level and sex tables there. Okay, so what if we want to look at this and maybe a little bit different way? Well, we can actually add what’s called the F table function to this object or rather look at this table object through the eyes of the F table function, so it’s just F table, and then just enter the name of that object. We created just a second ago called table. Underscore two and let’s run this. Okay, so now we get the same data presented slightly differently. I think this is a little bit easier to read in many ways, and so instead of scrolling through all this, you have this one single table that has all the data that you need. This might not always be what you want, but in this case. This is pretty good, okay, so now let’s look at one other type of function you can use from Besar. That’s quite nice when it comes to doing two and three way tables and this one is called the X tabs or the cross tabulation function. Okay, this one is going to. You’re gonna specify the syntax a little bit different, but we’ll start off the same, so let’s call this new table. We’re creating table, underscore three and using the left-hand arrow to name it and well type in cross tabs or X tabs all lowercase, and as the first essentially argument, we are going to first enter the tilde symbol, and then we can just enter the variable names as follows, so let’s do job level. We don’t need to use the dollar sign and the the name of the data frame. In this particular way, we can specify the the table the two-way table using the X tabs function, so we’ll use job level and the next variable is sex, so we use a plus sign, followed by this name of the variable, and then we specified the name of the data frame using data equals and demo data, so this is saying both of these variables come from that demo data data frame. Okay, so this is getting a started here and so what we’re going to do is run this line. Okay, and let’s highlight just the name of table, underscore three and run that and here we have the crosstab function now. This is going to look you’re like. Oh, this is pretty much the same thing well. The difference is Crosstab’s is kind of nice because it actually puts the names of the variables at the top, whereas if you remember from the table function, sex and job level didn’t appear here, okay, so this is kind of a nice way to retain that information, so I tend to have cut. I’m kind of partial to the X, The X tabs function when doing this, and in fact, we can do the exact same thing as we did before using that prop table function if we want to look at the proportions and just type in the name of this new table object, we created a table, underscore three and run that whole thing and we get the same thing. Okay, so you can apply the same functions. I just showed you so now. How would you do Crosstab’s function with a three-way table? Well, it’s pretty simple, lets. Just start here, so lets. Copy this paste here. I’m going to call this now. Table four, It’s a name, it’s something different and so instead of we have job level plus sex and let’s do plus race/ethnicity as we did before, so we’re just adding a third variable there, and now let’s run this whole thing and so let’s go now and highlight just the table underscore four to look at this table and click run. Okay, and you’ll see that. Oh, look, we’ve got something that looks very similar as to before. Now, let’s do as we did before and do the F table and then enter as the sole argument, the name of this table object. We just created so F table to convert it into a more user-friendly format to read click. Run and look at that now. We’ve got a nice table. Now notice here, though, that this didn’t, they were not actually when we’re doing these subsequent steps after we create this table like if we just click on table for underscore four again and just highlight that it doesn’t save it as this. If you wanted to save this format, you just created. You’d have to rename or overwrite or create a new table, so I’m going to override the existing table for using this F table function here and click run and then now let’s run table, underscore four and see what we get. Okay, now it’s associated with this format. Okay, so this is just a quick way. There’s many other ways you could actually do tables and our these are two of the functions that come ready with base are, and so I highly recommend you use either table or the X tabs function, the X function. I think is a little bit nicer, but either one will work, all right. Thank you very much.