The five minutes you spend each week will provide you with a building block you can use in the next two hours at work. Review language basics, discover methods to improve existing R code, explore new and interesting features, and learn about useful development tools and libraries that will make your time programming with R that much more productive.
All series code samples can be downloaded at https://github.com/mnr/five-minutes-of-R.
Skill Level Intermediate
- [Instructor] Sometimes you just need a simple count of a categorical variable or a factor. And the function tabulation or table is exactly what you need. So let's take a look at table. Here it is, TABLE. Pretty simple. And let's tabulate chick weight diet. So its, chick weight, dollar sign, diet. And we can pull that up. And when I hit control return. What I get is a quick count of the number of observations for each diet.
And each diet is numbered one, two, three and four. So you can see that for the first diet, we have 220 observations. Now we can make that table look a little bit more understandable. Let's use the DNN parameter to change the title. DNN equals and I give it one title for each observation. So this case, we've got one observation diet. And so I can say observations per diet.
And I hit control return. And you can see that I now have a table that's been titled with observations per diet. There's a couple more things we can do to clean up how table works. Or to make more sophisticated tables. I'm going to use the width command. WITH and then I say with this dataset, in this case, chick weight, I would like to use table. So now I've got table embedded inside of width.
And inside of table, I'm going to select the weight. And diet parameters. So let's go ahead and also select what we'd like to have for a title. So I'm going to put in DNN equals and in this case, I'll have two observations so I can use two titles. So I use the catenate and I'll put in the first one would be, let's take C, WEIGHT and the second title will be C diet, IET.
And when I hit control return. And we'll scroll up here and take a look at the top of this table, what we'll find is that we now have a table that's labeled with C weight or the weights for each chick across for each diet. So we can see that for diet one, we have one chick that weighed 35 and for diet one, we also have two chicks that weighed 39. Or two observations that weighed 39. So table is a real quick command that will give us a summary of factors inside data.