 Research tips from Monica Wahee of DeathWench Professional Services. Visit us at www.deathwench.com and let DeathWench do your data. Hi, it's Monica Wahee again talking about data. If you use SQL, you are used to doing select commands with the group by function. That way you can make means or counts by group. In R, we use the aggregate command for that. Let me show you. So here is our code. See that I read in an analytic file that was derived from the behavioral risk factor surveillance system data known as BRFSS data. I restricted this data set to only veterans. I'll be showing you a demonstration using this data set. Here is the first variable we are working with called sleep time 2. This variable is for sleep duration, like how long someone sleeps per night. Here is our grouping variable, which is sex. It is coded one for men and two for women. If you want to know why it's coded one for men, well, you'll just have to take my linda.com course. So let's get to the command. Here it is aggregate. The first argument as I showed you is our sleep duration variable. That means whatever function we call up mean standard deviation median. It will be doing that function to this variable. Next we have the buy command. We do buy equals then list then the grouping variable. Theoretically, you could put several grouping variables separated by commas. If you did that, R would give you mean sleep durations by each of the categorical variables. But that's a lot of output. So for this demonstration, we'll just stick to one on the list. Who said statistics isn't fun? I'll show you fun. Here's the next part of the aggregate command. I'm sorry, I hate to say that fun stands for function. But the good news is that you can change the function by designating a different fun. Here we designate a mean. So that's the function here. And I always throw this in at the end of my aggregate command. That is na.rm equals true. What this means is you are saying to R. R, please remove the na's from my calculation of mean. SAS does this automatically, but with R you have to tell it to do it. And then it will run. So let's run it. See the console? Here are the groups. Group one is men and group two is women, like I said. And here is x, which is the sleep time two variable or mean sleep duration per group. Hey, it looks like women sleep less than men. I wonder why? Maybe the men wake them up with their snoring. Hypothesis time? Just kidding. Or maybe I'm not kidding. Maybe we should look into it. But anyway, you can use aggregate for a lot of fun things, meaning a lot of functions. For instance, here's the same code, but this time the fun, or function, is standard deviation, indicated by fun equals SD. Let's run this. And look at that. Women even have a higher standard deviation than men in this data set. Scary. What are fun things you can do with aggregate? Minimum and maximum are some of the other fun ones. If you want to go further with R, check out the links in the description to this video, including my Lynda.com courses. And have a fun day! If you like these research tips, visit us at DeathWench.com and let DeathWench do your data.