Lab 5 Frequency Table

5.1 Introduction

Categorical data items are normally reported in frequency tables and crosstabs where the counts for a particular item are displayed. The only difference between these two types of tables is in the number of dimensions they display, frequency tables display only a single variable while a crosstab displays two variables. Both of these types of tables are commonly used to display polling data during the run-up to an election and would list things like the number of voters who would support some proposition (frequency table) or that same data broken out by party affiliation, sex, age, or some other category (crosstab). This lab explores both types of table.


5.2 Frequency Table

A frequency table is a one-dimensional table that lists a count of the number of times that some categorical data item appears in a vector. As an example, consider the following table which lists the number of cars for each number of cylinders in the mtcars data frame.

table(mtcars$cyl)
#> 
#>  4  6  8 
#> 11  7 14

This table shows that 11 cars in the data frame had 4 cylinders, 7 had 6 cylinders, and 14 had eight cylinders.

Frequency tables are only useful for categorical data-type items. To illustrate why this is true, imagine creating a survey for all of the students at the University of Arizona and including “age” (continuous-type data) as one of the survey questions. Attempting to create a frequency table for the ages of the respondents would have, potentially, more than 65 columns since student ages would range from about 15 to more than 80 and each column would report the number of students for that particular age. While R could create a frequency table that large it would have so many columns that it would be virtually unusable. Normally, if continuous-type data need to be displayed in a table the data are grouped in some way, like ages 15-19, 20-24, etc, so there would be a manageable number of group counts to display.

5.2.1 Demonstration: Frequency Table

The following script creates a frequency table.

  • Line 2: Create a simple frequency table listing the number of cars in the mtcars data frame by the forward gears.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENvdW50IG9mIGNhcnMgYnkgZ2VhcnNcbnRhYmxlKG10Y2FycyRnZWFyKVxuIn0=

5.2.2 Guided Practice: Frequency Table

Using the chickwts data frame, create a frequency table for feed.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIyBObyBwcmUtZXhlcmNpc2UgY29kZSBmb3IgdGhpcyBleGVyY2lzZVxuIiwic2FtcGxlIjoiIyBDcmVhdGUgYSBmcmVxdWVuY3kgdGFibGUgZm9yIGZlZWQgaW4gdGhlIGNoaWNrd3RzIGRhdGEgZnJhbWUuXG5cbiIsInNvbHV0aW9uIjoiXG50YWJsZShjaGlja3d0cyRmZWVkKVxuIiwic2N0IjoiXG5leDFuZXEgPC0gXCJDaGVjayB0aGF0IHRoZSB2YXJpYWJsZSBmb3IgdGhlIHRhYmxlIGZ1bmN0aW9uIGlzIGNoaWNrd3RzJGZlZWQuXCJcblxuZXgoKSAlPiUgXG4gIGNoZWNrX2Z1bmN0aW9uKFwidGFibGVcIikgJT4lIFxuICBjaGVja19hcmcoXCIuLi5cIikgJT4lIFxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXgxbmVxKVxuXG5zdWNjZXNzX21zZyhcIkdvb2Qgd29yayEgQSBmcmVxdWVuY3kgdGFibGUgaXMgYSBmb3VuZGF0aW9uYWwgY2F0ZWdvcmljYWwgZGF0YSBhbmFseXNpcyB0b29sIVwiKVxuIn0=

5.2.3 Activity: Frequency Table

Using the cafe data frame, generate a frequency table for meal. Copy/paste the table in the deliverable document for this lab.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuY2FmZSA8LSByZWFkLmNzdignaHR0cHM6Ly9sYWJzLmJhc3YzMTYuY29tL2NhZmUuY3N2JylcbiIsInNhbXBsZSI6IiMgVXNpbmcgdGhlIGNhZmUgZGF0YSBmcmFtZSwgZ2VuZXJhdGUgYSBmcmVxdWVuY3kgdGFibGUgZm9yIG1lYWwuXG5cblxuIn0=

5.3 Margins

It is often useful to include the total number of items counted in a frequency table and that is provided by the addmargins function. As an example, consider the following table which is the same frequency table shown above but includes the total number of cars in the mtcars data frame.


addmargins(table(mtcars$cyl))
#> 
#>   4   6   8 Sum 
#>  11   7  14  32

5.3.1 Demonstration: Margins

The following script creates a frequency table with margins

  • Line 2: Create a frequency table with margins listing the number of cars in the mtcars data frame by the forward gears.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENvdW50IG9mIGNhcnMgYnkgZ2VhcnNcbmFkZG1hcmdpbnModGFibGUobXRjYXJzJGdlYXIpKVxuIn0=

5.3.2 Guided Practice: Margins

Using the chickwts data frame, create a frequency table with margins for feed.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIyBObyBwcmUtZXhlcmNpc2UgY29kZSBmb3IgdGhpcyBleGVyY2lzZVxuIiwic2FtcGxlIjoiIyBDcmVhdGUgYSBmcmVxdWVuY3kgdGFibGUgd2l0aCBtYXJnaW5zIGZvciBmZWVkIGluIHRoZSBjaGlja3d0cyBkYXRhIGZyYW1lLlxuXG4iLCJzb2x1dGlvbiI6IlxuYWRkbWFyZ2lucyh0YWJsZShjaGlja3d0cyRmZWVkKSlcbiIsInNjdCI6IlxuZXgyMW5lcSA8LSBcIkNoZWNrIHRoYXQgdGhlIGFkZG1hcmdpbnMgZnVuY3Rpb24gY2FsbHMgdGFibGUoY2hpY2t3dHMkZmVlZCkuXCJcblxuZXgoKSAlPiVcbiAgY2hlY2tfZnVuY3Rpb24oXCJhZGRtYXJnaW5zXCIpICU+JVxuICBjaGVja19yZXN1bHQoKSAlPiVcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4MjFuZXEpXG5cbnN1Y2Nlc3NfbXNnKFwiUGVyZmVjdCEgTWFyZ2lucyBwcm92aWRlIGEgbGl0dGxlIGV4dHJhIGluZm9ybWF0aW9uIHRvIGEgZnJlcXVlbmN5IHRhYmxlIVwiKVxuIn0=

5.3.3 Activity: Margins

Using the cafe data frame, generate a frequency table for meal, include the margins on this table. Copy/paste the table in the deliverable document for this lab.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuY2FmZSA8LSByZWFkLmNzdignaHR0cHM6Ly9sYWJzLmJhc3YzMTYuY29tL2NhZmUuY3N2JylcbiIsInNhbXBsZSI6IiMgVXNpbmcgdGhlIGNhZmUgZGF0YSBmcmFtZSwgZ2VuZXJhdGUgYSBmcmVxdWVuY3kgdGFibGUgZm9yIG1lYWwsIGluY2x1ZGUgdGhlIG1hcmdpbnMgb24gdGhpcyB0YWJsZS5cblxuXG4ifQ==

5.4 Proportion Table (Proptable)

Occasionally, researchers prefer to present percentages rather than raw numbers since those are easier to quickly interpret. Here is a proportion table of the number of cylinders for cars in the mtcars data frame.


prop.table(table(mtcars$cyl)) * 100
#> 
#>      4      6      8 
#> 34.375 21.875 43.750

Thus, about 44% of the cars have eight cylinders.

5.4.1 Demonstration: Proportion Table (Proptable)

The following script creates a proportion table with the results presented in percent format.

  • Line 2: Create a simple proportion table listing the number of cars in the mtcars data frame by the forward gears.
  • Line 4: Multiply all values in the proportion table by 100 to make it a percentage.

This demonstration contains two different examples of generating a proportion table.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFByb3BvcnRpb24gdGFibGVcbnByb3AudGFibGUodGFibGUobXRjYXJzJGdlYXIpKVxuXG4jIE11bHRpcGx5IGFsbCBwcm9wb3J0aW9ucyBieSAxMDAgdG8gbWFrZSB0aGVtIHBlcmNlbnRhZ2VzXG5wcm9wLnRhYmxlKHRhYmxlKG10Y2FycyRnZWFyKSkgKiAxMDBcbiJ9

5.4.2 Guided Practice: Proportion Table (Proptable)

Using the chickwts data frame, create a proportion table for feed. The proportions should be multiplied by 100.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIyBObyBwcmUtZXhlcmNpc2UgY29kZSBmb3IgdGhpcyBleGVyY2lzZVxuIiwic2FtcGxlIjoiIyBDcmVhdGUgYSBwcm9wb3J0aW9uIHRhYmxlIGZvciBmZWVkIGluIHRoZSBjaGlja3d0cyBkYXRhIGZyYW1lLlxuXG4iLCJzb2x1dGlvbiI6IlxucHJvcC50YWJsZSh0YWJsZShjaGlja3d0cyRmZWVkKSkgKiAxMDBcbiIsInNjdCI6IlxuZXgzMW5lcSA8LSBcIkNoZWNrIHRoYXQgdGhlIHByb3AudGFibGUgZnVuY3Rpb24gY2FsbHMgdGFibGUoY2hpY2t3dHMkZmVlZCkuXCJcbmV4MzJuZXEgPC0gXCJDaGVjayB0aGF0IHRoZSByZXN1bHRzIG9mIHRoZSBwcm9wLnRhYmxlIGZ1bmN0aW9uIGFyZSBtdWx0aXBsaWVkIGJ5IDEwMC5cIlxuXG5leCgpICU+JVxuICBjaGVja19mdW5jdGlvbihcInByb3AudGFibGVcIikgJT4lXG4gIGNoZWNrX3Jlc3VsdCgpICU+JVxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXgzMW5lcSlcblxuZXgoKSAlPiVcbiAgY2hlY2tfY29kZShcIi4rWypdXFxcXHMqMTAwXCIsIG1pc3NpbmdfbXNnID0gZXgzMm5lcSlcblxuc3VjY2Vzc19tc2coXCJQZXJmZWN0ISBBIHByb3BvcnRpb24gdGFibGUgZm9ybWF0dGVkIGxpa2UgdGhpcyBpcyBhIHZhbHVhYmxlIHRvb2whXCIpXG4ifQ==

5.4.3 Activity: Proportion Table (Proptable)

Using the cafe data frame, generate a proportion table (proptable) for meal. The proportions should be multiplied by 100. Copy/paste the table in the deliverable document for this lab.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuY2FmZSA8LSByZWFkLmNzdignaHR0cHM6Ly9sYWJzLmJhc3YzMTYuY29tL2NhZmUuY3N2JylcbiIsInNhbXBsZSI6IiMgVXNpbmcgdGhlIGNhZmUgZGF0YSBmcmFtZSwgZ2VuZXJhdGUgYSBnZW5lcmF0ZSBhIHByb3BvcnRpb24gdGFibGUgKHByb3B0YWJsZSkgZm9yIG1lYWwuIFRoZSBwcm9wb3J0aW9ucyBzaG91bGQgYmUgbXVsdGlwbGllZCBieSAxMDAuXG5cblxuIn0=

5.5 Crosstab

A crosstab (sometimes called a contingency table or pivot table), is a table of frequencies used to display the relationship between two nominal or ordinal variables. As an example of a crosstab, consider a table from the mtcars data frame that lists the number of cars by forward gears and the number of cylinders.


xtabs(~gear+cyl, data=mtcars)
#>     cyl
#> gear  4  6  8
#>    3  1  2 12
#>    4  8  4  0
#>    5  2  1  2

In this case, one car had a four cylinder engine and three forward gears while 12 cars had eight cylinders and three forward gears. By using a crosstab, a researcher can determine the frequency of some incident (number of cars) by two different criteria (gears and cylinders).

Here is a second example from the esoph data frame.


xtabs(~agegp+ncases, data=esoph)
#>        ncases
#> agegp    0  1  2  3  4  5  6  8  9 17
#>   25-34 14  1  0  0  0  0  0  0  0  0
#>   35-44 10  2  2  1  0  0  0  0  0  0
#>   45-54  3  2  2  2  3  2  2  0  0  0
#>   55-64  0  0  2  4  3  2  2  1  2  0
#>   65-74  1  4  2  2  2  2  1  0  0  1
#>   75+    1  7  3  0  0  0  0  0  0  0

Notice that there were few cases of esophageal cancer among people under the age of 45 but the number of cases increased between the ages of 45 and 74, with a peak in the 55-64 age group.

5.5.1 Demonstration: Crosstab

The following script demonstrates how to create a crosstab.

  • Line 2: The xtabs command (for “Cross Tabs”) creates a crosstab for the variables entered. It is important to notice the tilde character in this function. In many R commands the tilde is used to separate two parts of a formula. The part before the tilde are the data values to be acted upon and the second part are the grouping vectors. In Line 2, there are no data values specified so R will simply count the number of times the various groups show up. The “row” group is listed first and then the “column” group. For example, “gear 3 - cylinder 8” appear 12 times in the data frame.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENvdW50IG9mIGdlYXJzIGFuZCBjeWxpbmRlcnNcbnh0YWJzKH5tdGNhcnMkZ2VhcittdGNhcnMkY3lsKVxuIn0=

5.5.2 Guided Practice: Crosstab

Using the esoph data frame, create a crosstab for tobgp (tobacco group) and alcgp (alcohol group).

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIyBObyBwcmUtZXhlcmNpc2UgY29kZSBmb3IgdGhpcyBleGVyY2lzZVxuIiwic2FtcGxlIjoiIyBDcmVhdGUgYSBjcm9zc3RhYnMgdGFibGUgZm9yIHRvYmdwIGFuZCBhbGNncCBpbiB0aGUgZXNvcGggZGF0YSBmcmFtZS5cbiIsInNvbHV0aW9uIjoiXG54dGFicyh+ZXNvcGgkdG9iZ3ArZXNvcGgkYWxjZ3ApXG4iLCJzY3QiOiJcbmV4NDFuZXEgPC0gXCJUaGVyZSBzaG91bGQgYmUgYSB0aWxkZSBmb2xsb3dlZCBieSB0aGUgdHdvIGdyb3VwaW5nIHZhcmlhYmxlcy5cIlxuXG5leCgpICU+JSBcbiAgY2hlY2tfZnVuY3Rpb24oXCJ4dGFic1wiKSAlPiUgXG4gIGNoZWNrX2FyZyhcImZvcm11bGFcIikgJT4lIFxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXg0MW5lcSlcblxuc3VjY2Vzc19tc2coXCJQZXJmZWN0ISBUaGlzIHR5cGUgb2YgY3Jvc3N0YWIgaXMgdmVyeSBjb21tb25seSB1c2VkIVwiKVxuIn0=

5.5.3 Activity: Crosstab

Using the cafe data frame, create a crosstab of food and svc. Copy/paste the table in the deliverable document for this lab.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuY2FmZSA8LSByZWFkLmNzdignaHR0cHM6Ly9sYWJzLmJhc3YzMTYuY29tL2NhZmUuY3N2JylcbiIsInNhbXBsZSI6IiMgVXNpbmcgdGhlIGNhZmUgZGF0YSBmcmFtZSwgY3JlYXRlIGEgY3Jvc3N0YWIgb2YgZm9vZCBhbmQgc3ZjLlxuXG5cbiJ9

5.6 Multi-Dimensional Crosstab

A crosstab can contain more than two dimensions. As an example, consider the mtcars data frame. A researcher wanted to know if there is any relationship between the number of forward gears, the number of cylinders in the engine (that is, the size of the engine), and whether the car had a manual or automatic transmission. Here is the crosstab that was created.


xtabs(~cyl+gear+am, data=mtcars)
#> , , am = 0
#> 
#>    gear
#> cyl  3  4  5
#>   4  1  2  0
#>   6  2  2  0
#>   8 12  0  0
#> 
#> , , am = 1
#> 
#>    gear
#> cyl  3  4  5
#>   4  0  6  2
#>   6  0  2  1
#>   8  0  0  2

When am is 0 (which is the code for an automatic transmission) there were no cars with five forward gears and when am is 1 (manual transmission) there were no cars with three forward gears.

5.6.1 Demonstration: Multi-dimensional Crosstab

The following script demonstrates how to create a multi-dimensional crosstab.

  • Line 2: The xtabs command creates a crosstab for the variables entered. In this case, there is nothing on the left side of the tilde so R will return a count of the various categories. Since there are three grouping variables, gear, cyl, and am, R will count instances for all three variables. For example, “gear 4” and “cylinder 4” appear together 2 times when the transmission is 0 and 6 times when the transmission is 1. Note that a data = mtcars parameter is also specified so the $ operator does not need to be used for each variable. This makes the formula easier to read.
  • Line 5: This is another example of multi-dimensional crosstab.

This demonstration contains two different examples of generating a multi-dimensional crosstab.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIENvdW50IG9mIGdlYXJzIGFuZCBjeWxpbmRlcnMgYnkgdHJhbnNtaXNzaW9uXG54dGFicyh+Z2VhcitjeWwrYW0sIGRhdGEgPSBtdGNhcnMpXG5cbiMgQ291bnQgb2YgY2FyYnMgYW5kIGN5bGluZGVycyBieSBlbmdpbmUgdHlwZVxueHRhYnMofmNhcmIrY3lsK3ZzLCBkYXRhID0gbXRjYXJzKVxuIn0=

5.6.2 Guided Practice: Multi-dimensional Crosstab

Using the esoph data frame, create a crosstab for tobgp (tobacco group), alcgp (alcohol group), and agegp (age group).

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIyBObyBwcmUtZXhlcmNpc2UgY29kZSBmb3IgdGhpcyBleGVyY2lzZVxuIiwic2FtcGxlIjoiIyBDcmVhdGUgYSBjcm9zc3RhYnMgdGFibGUgZm9yIHRvYmdwLCBhbGNncCwgYW5kIGFnZWdwIGluIHRoZSBlc29waCBkYXRhIGZyYW1lLlxuIiwic29sdXRpb24iOiJcbnh0YWJzKH50b2JncCthbGNncCthZ2VncCwgZGF0YT1lc29waClcbiIsInNjdCI6IlxuZXg1MW5lcSA8LSBcIlRoZXJlIHNob3VsZCBiZSBhIHRpbGRlIGZvbGxvd2VkIGJ5IHRoZSB0aHJlZSBncm91cGluZyB2YXJpYWJsZXMuXCJcbmV4NTJuZXEgPC0gXCJCZSBzdXJlIGVzb3BoIGlzIHNwZWNpZmllZCBhcyB0aGUgZGF0YS5cIlxuXG5mdW5zdGF0ZSA8LSBleCgpICU+JSBjaGVja19mdW5jdGlvbihcInh0YWJzXCIpXG5mdW5zdGF0ZSAlPiUgXG4gIGNoZWNrX2FyZyhcImZvcm11bGFcIikgJT4lIFxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXg1MW5lcSlcblxuZnVuc3RhdGUgJT4lIFxuICBjaGVja19hcmcoXCJkYXRhXCIpICU+JSBcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4NTJuZXEpXG5cbnN1Y2Nlc3NfbXNnKFwiUGVyZmVjdCEgQSBtdWx0aS1kaW1lbnNpb25hbCBjcm9zc3RhYiBpcyB2ZXJ5IHVzZWZ1bCFcIilcbiJ9

5.6.3 Activity: Multi-dimensional Crosstab

Using the cafe data frame, create a crosstab of food and svc when grouped by sex. Copy/paste the table in the deliverable document for this lab.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuY2FmZSA8LSByZWFkLmNzdignaHR0cHM6Ly9sYWJzLmJhc3YzMTYuY29tL2NhZmUuY3N2JylcbiIsInNhbXBsZSI6IiMgVXNpbmcgdGhlIGNhZmUgZGF0YSBmcmFtZSwgY3JlYXRlIGEgY3Jvc3N0YWIgb2YgZm9vZCBhbmQgc3ZjIHdoZW4gZ3JvdXBlZCBieSBzZXguXG5cblxuIn0=

5.7 Calculated Crosstab

Each of the crosstabs presented so far have displayed only counts of data; however, by using the aggregate function, a crosstab can also display calculated values, like the mean temperature for each summer month in the airquality data frame.


aggregate(Temp ~ Month, 
                data = airquality, 
                FUN = mean)
#>   Month     Temp
#> 1     5 65.54839
#> 2     6 79.10000
#> 3     7 83.90323
#> 4     8 83.96774
#> 5     9 76.90000

Though the above table has a number of calculated values the R command is only one line long.

As another example, the mean horsepower for automobiles can be calculated by the number of forward gears and engine cylinders from the mtcars data frame.


aggregate(hp ~ gear + cyl, 
          data = mtcars, 
          FUN = mean)
#>   gear cyl       hp
#> 1    3   4  97.0000
#> 2    4   4  76.0000
#> 3    5   4 102.0000
#> 4    3   6 107.5000
#> 5    4   6 116.5000
#> 6    5   6 175.0000
#> 7    3   8 194.1667
#> 8    5   8 299.5000

5.7.1 Demonstration: Calculated Crosstab

The following script demonstrates how to create a calculated crosstab.

  • Line 2: This line uses the aggregate command to calculate the mean displacement for each category of cylinders. Notice that this command has a tilde formula where displacement will be aggregated for each category of cylinders. The values to be calculated comes first (displacement) and the grouping variable comes second (cylinder). Next is the name of the dataset used, mtcars. Finally, the statistical function is listed as FUN = mean. Note that the key word FUN is in capital letters. This aggregate function determines that four cylinder cars have a mean displacement of just over 105 cubic inches.
  • Line 5: This line also calculates the mean displacement but groups the result by both number of cylinders and number of forward gears, creating a multi-dimensional calculated crosstab. It calculates that the mean displacement for four cylinder cars with three forward gears is just over 120 cubic inches.

This demonstration contains two different examples of generating a calculated crosstab.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIE1lYW4gb2YgZGlzcGxhY2VtZW50IGJ5IGN5bGluZGVyc1xuYWdncmVnYXRlKGRpc3B+Y3lsLCBkYXRhID0gbXRjYXJzLCBGVU4gPSBtZWFuKVxuXG4jIE1lYW4gb2YgZGlzcGxhY2VtZW50IGJ5IGN5bCBhbmQgZ2VhcnNcbmFnZ3JlZ2F0ZShkaXNwfmN5bCtnZWFyLCBkYXRhID0gbXRjYXJzLCBGVU4gPSBtZWFuKVxuIn0=

5.7.2 Guided Practice: Calculated Crosstab

Using the npk data frame, create a calculated crosstab for N (nitrogen), P (phosphate), and K (potassium). Aggregate the mean yield for all three vectors. Notice that the vector names are capital letters.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIyBObyBwcmUtZXhlcmNpc2UgY29kZSBmb3IgdGhpcyBleGVyY2lzZVxuIiwic2FtcGxlIjoiIyBDcmVhdGUgYSBjYWxjdWxhdGVkIGNyb3NzdGFiIGZvciB0aGUgbWVhbiB5aWVsZCBmb3IgTiwgUCwgYW5kIEsgaW4gdGhlIG5wayBkYXRhIGZyYW1lLlxuXG4iLCJzb2x1dGlvbiI6IlxuYWdncmVnYXRlKHlpZWxkfk4rUCtLLCBkYXRhID0gbnBrLCBGVU4gPSBtZWFuKVxuIiwic2N0IjoiXG5leDYxbmVxIDwtIFwiWWllbGQgc2hvdWxkIGJlIGZvbGxvd2VkIGJ5IGEgdGlsZGUgdGhlbiB0aGUgdGhyZWUgZ3JvdXBpbmcgdmFyaWFibGVzLlwiXG5leDYybmVxIDwtIFwiQmUgc3VyZSBucGsgaXMgc3BlY2lmaWVkIGFzIHRoZSBkYXRhLlwiXG5leDYzbmVxIDwtIFwiQmUgc3VyZSBtZWFuIGlzIHNwZWNpZmllZCBhcyB0aGUgZnVuY3Rpb24uXCJcblxuc3RhdGU2IDwtIGV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwiYWdncmVnYXRlXCIpXG5zdGF0ZTYgJT4lIFxuICBjaGVja19hcmcoXCJmb3JtdWxhXCIpICU+JSBcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4NjFuZXEpXG5cbnN0YXRlNiAlPiUgXG4gIGNoZWNrX2FyZyhcImRhdGFcIikgJT4lIFxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXg2Mm5lcSlcblxuc3RhdGU2ICU+JSBcbiAgY2hlY2tfYXJnKFwiRlVOXCIpICU+JSBcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4NjNuZXEpXG5cbnN1Y2Nlc3NfbXNnKFwiUGVyZmVjdCEgQ2FsY3VsYXRpbmcgdGhlIG1lYW4gb3Igb3RoZXIgdmFsdWUgbWFrZXMgY3Jvc3N0YWJzIG1vcmUgdXNlZnVsIVwiKVxuIn0=

5.7.3 Activity: Calculated Crosstab

Using the cafe data frame, create a calculated crosstab of the mean tip when grouped by food and svc. Copy/paste the table in the deliverable document for this lab.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuY2FmZSA8LSByZWFkLmNzdignaHR0cHM6Ly9sYWJzLmJhc3YzMTYuY29tL2NhZmUuY3N2JylcbiIsInNhbXBsZSI6IiMgVXNpbmcgdGhlIGNhZmUgZGF0YSBmcmFtZSwgY3JlYXRlIGEgY2FsY3VsYXRlZCBjcm9zc3RhYiBvZiB0aGUgbWVhbiB0aXAgZm9yIGZvb2QgYW5kIHN2Yy5cbiJ9

5.8 Rounding

In order to facilite understanding, R makes it easy for researchers to round the results of calculations to whatever level is desired. The R command to round a number is round() where the number to be rounded is listed first in the parenthesis and the number of decimal places is listed second. So round(1.3498, 2) would be rounded to 1.35 and round(1.3498, 1) would be rounded to 1.3. The number to be rounded can be a calculated value rather than just a plain number. It is important to note that even if the displayed value is rounded, R still uses the full decimal number for calculations. Also, when rounding a 5, “banker’s” rounding is used, that is, “go to the even digit.” So, 2.5 is rounded to 2.0 but 3.5 is rounded to 4.0.

5.8.1 Demonstrate: Rounding

As an example, here is the same aggregate command used in the previous example, but with the results rounded. Notice how the aggregate command is the same, but it is wrapped in a round function to round off the calculations. In the first example the result is rounded to three decimal places and in the second example to two decimal places.

This demonstration contains two different examples of rounding.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIE1lYW4gb2YgZGlzcGxhY2VtZW50IGJ5IGN5bGluZGVyc1xucm91bmQoYWdncmVnYXRlKGRpc3B+Y3lsLCBkYXRhID0gbXRjYXJzLCBGVU4gPSBtZWFuKSwgZGlnaXRzID0gMylcblxuIyBNZWFuIG9mIGRpc3BsYWNlbWVudCBieSBjeWwgYW5kIGdlYXJzXG5yb3VuZChhZ2dyZWdhdGUoZGlzcH5jeWwrZ2VhciwgZGF0YSA9IG10Y2FycywgRlVOID0gbWVhbiksIGRpZ2l0cyA9IDIpXG4ifQ==

5.8.2 Guided Practice: Rounding

Using the infert data frame, create a calculated crosstab for case, induced, and spontaneous. Aggregate the mean age for all three vectors. Round the results to two decimal places.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIyBObyBwcmUtZXhlcmNpc2UgY29kZSBmb3IgdGhpcyBleGVyY2lzZVxuIiwic2FtcGxlIjoiIyBDcmVhdGUgYSBjYWxjdWxhdGVkIGNyb3NzdGFiIG9mIGFnZSByb3VuZGVkIHRvIHR3byBkZWNpbWFsIHBsYWNlcyBmb3IgY2FzZSwgaW5kdWNlZCwgYW5kIHNwb250YW5lb3VzIGluIHRoZSBpbmZlcnQgZGF0YSBmcmFtZS5cblxuIiwic29sdXRpb24iOiJcbnJvdW5kKGFnZ3JlZ2F0ZShhZ2V+Y2FzZStpbmR1Y2VkK3Nwb250YW5lb3VzLCBkYXRhPWluZmVydCwgRlVOPW1lYW4pLCBkaWdpdHMgPSAyKVxuIiwic2N0IjoiXG5leDcxbmVxIDwtIFwiSW4gYWdncmVnYXRlLCBhZ2Ugc2hvdWxkIGJlIGZvbGxvd2VkIGJ5IGEgdGlsZGUgdGhlbiB0aGUgdGhyZWUgZ3JvdXBpbmcgdmFyaWFibGVzLlwiXG5leDcybmVxIDwtIFwiSW4gYWdncmVnYXRlLCBiZSBzdXJlIGluZmVydCBpcyBzcGVjaWZpZWQgYXMgdGhlIGRhdGEuXCJcbmV4NzNuZXEgPC0gXCJJbiBhZ2dyZWdhdGUsIGJlIHN1cmUgbWVhbiBpcyBzcGVjaWZpZWQgYXMgdGhlIEZVTi5cIlxuZXg3NG5lcSA8LSBcIkNoZWNrIHRvIGJlIHN1cmUgeW91IGhhdmUgcm91bmRlZCB0byB0aGUgY29ycmVjdCBudW1iZXIgb2YgZGlnaXRzLlwiXG5cbnN0YXRlNzEgPC0gZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJhZ2dyZWdhdGVcIilcbnN0YXRlNzEgJT4lIFxuICBjaGVja19hcmcoXCJmb3JtdWxhXCIpICU+JSBcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4NzFuZXEpXG5cbnN0YXRlNzEgJT4lIFxuICBjaGVja19hcmcoXCJkYXRhXCIpICU+JSBcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4NzJuZXEpXG5cbnN0YXRlNzEgJT4lIFxuICBjaGVja19hcmcoXCJGVU5cIikgJT4lIFxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXg3M25lcSlcblxuc3RhdGU3MiA8LSBleCgpICU+JSBjaGVja19mdW5jdGlvbihcInJvdW5kXCIpXG5zdGF0ZTcyICU+JSBcbiAgY2hlY2tfcmVzdWx0KCkgJT4lXG4gIGNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDc0bmVxKVxuXG5cbnN1Y2Nlc3NfbXNnKFwiR29vZCBXb3JrISBBIHJvdW5kZWQgbWVhbiBpcyBtdWNoIGVhc2llciB0byB1c2UhXCIpXG4ifQ==

5.8.3 Activity: Rounding

Using the cafe data frame, create a calculated crosstab of the mean tip when grouped by food and svc. Round the calculated values to two decimal places. Copy/paste the table in the deliverable document for this lab.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuY2FmZSA8LSByZWFkLmNzdignaHR0cHM6Ly9sYWJzLmJhc3YzMTYuY29tL2NhZmUuY3N2JylcbiIsInNhbXBsZSI6IiMgVXNpbmcgdGhlIGNhZmUgZGF0YSBmcmFtZSwgY3JlYXRlIGEgY2FsY3VsYXRlZCBjcm9zc3RhYiBvZiB0aGUgbWVhbiB0aXAgZm9yIGZvb2QgYW5kIHN2Yywgcm91bmRlZCB0byB0d28gZGVjaW1hbCBwbGFjZXMuXG5cblxuIn0=

5.9 Deliverable

Complete the activities in this lab and consolidate the responses into a single document. Name the document with your name and “Lab 5,” like “George Self Lab 5” and submit that document for grade.