

{"id":2868,"date":"2017-06-16T08:54:55","date_gmt":"2017-06-16T03:24:55","guid":{"rendered":"http:\/\/data-flair.training\/blogs\/?p=2868"},"modified":"2021-08-25T18:20:55","modified_gmt":"2021-08-25T12:50:55","slug":"r-contingency-tables","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/","title":{"rendered":"Introduction to Contingency Tables in R &#8211; A Vital Booster for Mastering R"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:2323,&quot;href&quot;:&quot;https:\\\/\\\/cran.r-project.org\\\/faqs.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250923124055\\\/https:\\\/\\\/cran.r-project.org\\\/faqs.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-11 03:56:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-15 00:40:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-18 08:45:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-28 05:36:00&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-13 09:14:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-26 22:34:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-31 01:04:14&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-04 14:16:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-09 04:52:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 15:38:19&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-17 01:46:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 23:32:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-25 00:08:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 19:55:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-07 12:43:14&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-11 10:58:32&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-15 17:00:28&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-22 17:17:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-27 21:34:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-31 01:29:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-05 21:59:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-09 19:42:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-23 09:33:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-28 20:48:13&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-04 14:40:06&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-14 05:29:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-19 03:33:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-28 21:46:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-04 06:51:21&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-04 06:51:21&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>This R tutorial is all about contingency tables in R. First of all, we will discuss the introduction to R contingency tables and different ways to create contingency tables in R.\u00a0 And, after completing this tutorial, you will thoroughly understand the complex tables\/ flat tables, cross tabulation and recreating original data from contingency tables in R.<\/p>\n<h2>What are Contingency Tables in R?<\/h2>\n<p>A contingency table is particularly useful when a large number of observations need to be condensed into a smaller format whereas a complex (flat) table is a type of contingency table that is used when creating just one single table as opposed to multiple ones.<\/p>\n<p>You can <em>manipulate, alter,<\/em> and <em>produce<\/em> table objects using the <strong>table()<\/strong> command to summarize a data sample including\u00a0<em>vector, matrix, list,<\/em> and <em>data frame objects<\/em>. You can also create a few special kinds of table objects, like <em>contingency tables<\/em> and <em>complex (flat) contingency tables<\/em> using table() command.<\/p>\n<p>Additionally, you can also use cross-tabulation to reassemble data into a tabular format, as required.<\/p>\n<p><em><strong>Do you know about <a href=\"https:\/\/data-flair.training\/blogs\/object-oriented-programming-in-r\/\">Object Oriented Programming in R<\/a><\/strong><\/em><\/p>\n<h3>Making Contingency Tables in R<\/h3>\n<p>A contingency table is a way to redraw data and assemble it into a table. And, it shows the layout of the original data in a manner that allows the reader to gain an overall summary of the original data. table() command can be used to create contingency tables in R because the command can handle data in simple vectors or more complex matrix and data frame objects. The more complex the original data, the more complex is the resulting contingency table.<\/p>\n<h3>Creating Contingency Tables from Vectors in R<\/h3>\n<p>Vector is the simplest data object from which you can create a contingency table. In the following example, you have a simple numeric vector of values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; #Author DataFlair\r\n&gt; vec = c(3,5,7,9,11,3,6,2,1,9,0,5,4)\r\n&gt; table(vec)\r\n&gt; sort(vec)<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60555 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables.jpg\" alt=\"Creating Contingency Tables\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/4.-Creating-Contingency-Tables-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>With the help of the sort() command, we are able to obtain a reordered set of data values. We can also compare the sorted values with the output that is obtained when the table() command is applied to it.<\/p>\n<p><em><strong>Explore the <a href=\"https:\/\/data-flair.training\/blogs\/r-numeric-and-character-functions\/\">Numeric and Character Functions in R<\/a><\/strong><\/em><\/p>\n<h3>Creating R Contingency Tables from Data<\/h3>\n<p>In this section, we will explain a simple example that provides a data frame containing numeric values in one column and also containing factors in two of its columns. These two columns of factors contain character variables.<\/p>\n<p><span style=\"font-weight: 400\">In order to create our contingency table from data, we will <em>make use of the table(), addmargins(), as.data.frame.matrix() and prop.table().<\/em> In the following example, the table() function returns a contingency table. Basically, it returns a tabular result of the categorical variables. <\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#Author DataFlair\r\nlibrary(dplyr)\r\ntoy_data = data.frame(c1 = sample(letters[1:5], 25, replace = TRUE), \r\n                      c2 = sample(LETTERS[1:5], 25, replace = TRUE))\r\n\r\nprint.data.frame(toy_data)\r\n\r\ntable0 &lt;- table(toy_data$c1, toy_data$c2)\r\nprint.table(table0)\r\n\r\ntable1 &lt;- as.data.frame.matrix(table0) # convert it to dataframe\r\nprint.data.frame(table1)\r\n\r\ntable2 &lt;- prop.table(table0, margin = 1) %&gt;% \r\n  as.data.frame.matrix() # convert it to dataframe   \r\n# have a look at the table\r\nprint.data.frame(table2, digits = 2)\r\n\r\ntable3 &lt;- prop.table(table0, margin = 2) %&gt;% \r\n  as.data.frame.matrix() # convert it to dataframe   \r\n# have a look at the table\r\nprint.data.frame(table3, digits = 2)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-60556 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data.jpg\" alt=\"Creating R contingency tables from data\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/5.-Creating-R-Contingency-Tables-from-Data-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>We obtain the following output:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-60871 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1.jpg\" alt=\"Create R contingency tables from data 1\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-60885\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1.jpg\" alt=\"Create R contingency tables from data 2\" width=\"1299\" height=\"984\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1-150x114.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1-300x227.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1-768x582.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1-1024x776.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Create-R-contingency-tables-from-data-2-1-520x394.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/h3>\n<h3>How to Create Custom Contingency Tables in R?<\/h3>\n<p>The contingency table in R can be created using only a part of the data which is in contrast with collecting data from all the rows and columns. In situations like these, we can perform a selection of each row and column that is to be used.<\/p>\n<p>You can create a custom contingency table in <a href=\"https:\/\/cran.r-project.org\/faqs.html\">R<\/a> using the following ways:<\/p>\n<ul>\n<li>Performing column selection for use in the contingency table.<\/li>\n<li>Performing selection of the rows that are to be used.<\/li>\n<li>Carrying out the rotation of data frames.<\/li>\n<li>Making use of data objects.<\/li>\n<li>Using data frames.<\/li>\n<\/ul>\n<p>We will learn about each of these in detail below:<\/p>\n<h4>1. Selecting Columns to Use in an R Contingency Table<\/h4>\n<p>With the help of table() command, we are able to specify the columns with which the contingency tables can be created. In order to do so, you only need to mention the name of vector objects as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">table(table1$A)<\/pre>\n<p>This can also be written as:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">table(table1[,1])<\/pre>\n<p>We obtain the following output when we run these commands in our RStudio:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60559 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1.jpg\" alt=\"Selecting columns to use in R contingency tables\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h4>2. Selecting Rows to Use in R Contingency Table<\/h4>\n<p>Rows form the basis of contingency tables. In order to select only certain rows, a different approach is to be adapted. This process requires the creation of an object that is of the matrix form through which you can derive a contingency table using rows that are present in the data frame of the contingency table.<\/p>\n<p><em><strong>Have you checked &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/r-data-frame\/\">R Data Frame Tutorial<\/a><\/strong><\/em><\/p>\n<h4>3. Rotating Data Frames in R<\/h4>\n<p>You can perform a rotation of the data, that is, transpose of the data using the <strong>t() <\/strong>command. This can be carried out as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; t(table1)\r\n<\/pre>\n<p>We obtain the following output for the transposed table &#8216;table1&#8217;:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60561 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating.jpg\" alt=\"Selecting rows to use in R contingency tables\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.3-Rotating-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h4>4. Creating Contingency Tables from Matrix Objects in R<\/h4>\n<p><span style=\"font-weight: 400\">For this section, we will create our matrix of bird observation data as follows:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">bird = matrix( c(47, 10, 40, 2, 5, 2, 19, 3, 5, 0, 2, 50, 0, 10, 7, 0, 46, 8, 4, 0, 9, 4, 0, 0, 2, 4 ,0, 6, 0,0), nrow=6, ncol=5,byrow = TRUE)                      # fill matrix by rows \r\n\r\ndimnames(bird) = list( c(\"Blackbird\", \"Chaffinch\", \"Great Tit\",\r\n                      \"House Sparrow\", \"Robin\", \"Song Thrush\"),         # row names \r\n                      c(\"Garden\", \"Hedgerow\", \"Parkland\", \r\n                        \"Pasture\", \"Woodland\"))                         #Column names\r\n<\/pre>\n<p><span style=\"font-weight: 400\">Our matrix \u201cbird\u201d looks like:<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60562 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP.jpg\" alt=\"Matrix bird output\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-Matrix-Bird-OP-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400\">Using the <\/span><strong>table()<\/strong><span style=\"font-weight: 400\"> command, you will get the result, shown in the following table:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;table(bird)<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60563 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table.jpg\" alt=\"table ()\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.4-table-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Understand the <a href=\"https:\/\/data-flair.training\/blogs\/r-matrix-functions\/\">R Matrix and Matrix Function in R<\/a><\/strong><\/em><\/p>\n<h4>5. Using Rows of a Data Frame in a Contingency Table<\/h4>\n<p>With the help of this matrix, you can create a contingency table by looking at the rows. However, if there is a data frame, the same cannot be done with the use of bracket convention.<\/p>\n<p>You can get it to work as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#Author DataFlair\r\ntable(as.matrix(bird)[1,], as.matrix(bird)[2,], \r\n      dnn = c(\"Garden\", \"Hedgerow\"))\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60565 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5.jpg\" alt=\"table input - R contingency tables\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400\">Furthermore, a new matrix can also be created on which the table() command is applied as follows:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#Author DataFlair\r\nbird.mat = as.matrix(bird)\r\ntable(bird.mat[1,], bird.mat[4,], dnn = c(\"Garden\", \"Hedgerow\")) \r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60567 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output.jpg\" alt=\"\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/6.5-table-output-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>Selecting Parts of R Table Object<\/h3>\n<p>In R, the special type of matrix is a table. Like handling matrix objects, you can also deal with the tables. This also covers extracting matrix objects which are similar to extraction of the table object.<\/p>\n<p>Below some commands\u00a0are listed for selecting parts of a table object:<\/p>\n<p><strong>&gt; str(pw.tab) &#8211;<\/strong> Examines the structure of the table object named tab.<\/p>\n<p><strong>&gt; pw.tab[1:3,] &#8211;<\/strong> Displays the first three rows of the contingency table.<\/p>\n<p><strong>&gt; pw.tab[1:3,1]<\/strong> &#8211; Displays the first three rows of the first column.<\/p>\n<p><strong>&gt; pw.tab[1:3,1:2]<\/strong> &#8211; Displays the first three rows of the first and second columns.<\/p>\n<p><strong>&gt; pw.tab[,\u2019hi\u2019]<\/strong> &#8211; Displays the column labeled hi.<\/p>\n<p><strong>&gt; pw.tab[1:3, c(\u2018hi\u2019, \u2018mid\u2019)]<\/strong> &#8211; Displays the first three rows of two of the columns.<\/p>\n<p><strong>&gt; pw.tab[1:3, c(\u2018mid\u2019, \u2018hi\u2019)]<\/strong> &#8211; Displays some of the columns in a new order.<\/p>\n<p><strong>&gt; pw.tab[,c(\u2018hi\u2019,3)]<\/strong> &#8211; Displays two columns using a mix of name and number.<\/p>\n<p><strong>&gt; length(pw.tab)<\/strong> &#8211; Displays the length of the table object.<\/p>\n<p>The first step is to create the table object using two of the columns to produce a simple contingency table. The <strong>str()<\/strong> command validates that the resulting object is a table. The table can be displayed much like a matrix by using square brackets to define rows and columns as required. The rows and columns can be specified as numbers or names (if appropriate), but you cannot mix names and numbers in the same command.<\/p>\n<p>The <strong>length()<\/strong> command produces a result that reflects the number of items in the table; this is similar to a matrix but different from a data frame (where the command produced a number of columns).<\/p>\n<h3>Converting an Object into a Table<\/h3>\n<p>As mentioned above, a table is a special type of data object which is similar to the matrix but also possesses several differences.<\/p>\n<p>We make use of the as.table() command for converting an object into a table. This is only valid, however, if the object is in the matrix form. In the case of a data frame, the object can be converted into the matrix and then it can be converted into the table. We will do the same in R as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; as.table(as.matrix(bird))<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60568 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8.jpg\" alt=\"as.table matrix - R contingency tables\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/8-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h4>Testing for R Table Objects<\/h4>\n<p>In order to validate if the object is of the table type, we use the <strong>is.table()<\/strong> command. The result of this command is Boolean, that is, we will obtain either TRUE or FALSE.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; is.table(bird)\r\n\r\n&gt; is.table(table0)\r\n<\/pre>\n<h3>Complex Tables\/ Flat Tables in R<\/h3>\n<p>Several rows and columns are subdivided to form a single table. This happens in a flat table. We can create the same using an alternative table() command. The ftable() command can be used in various ways such as:<\/p>\n<ul>\n<li>To construct a &#8220;Flat&#8221; contingency table in R, we can use the ftable() command.<\/li>\n<li>You can also make selective &#8220;Flat&#8221; tables in R.<\/li>\n<\/ul>\n<h4>Making \u201cFlat\u201d Contingency Tables in R<\/h4>\n<p>The &#8220;flat&#8221; contingency table in R can be created using the ftable() command as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#DataFlair\r\nftable(toy_data)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60573 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat.jpg\" alt=\"Flat contingency table\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/10.1-flat-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><br \/>\nWe can also create contingency tables in R with the help of the table() command. We can also specify two or more columns that are to be used in a table. For obtaining a slightly different custom output, we can use a different syntax. The general form of command is: <em>ftable(column.items~row.items, data = data.object)<\/em><\/p>\n<p>Wondering what this tilde (~) character means? Well, it is used for creating a formula in places where the left side of the symbol is containing variables in the form of row headings that are separated by the commas. The names of the vectors also form the row items. Before the ~ forms are the column specification whereas to the right, there are groupings of the table in the order of specification.<\/p>\n<p><em><strong>You must definitely check the <a href=\"https:\/\/data-flair.training\/blogs\/input-output-features-in-r\/\">Input\/ Output Features in R<\/a><\/strong><\/em><\/p>\n<h4>Testing R Flat Table Objects<\/h4>\n<p>For understanding the type of object that is being dealt with, we use the class() command. This command provides a unique label for each kind of the object. With the specification of the class of the object, R is capable of determining the type of object and also specifies the class of it.<\/p>\n<p>The command you can use for testing flat objects is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if(class(table1) == 'ftable') TRUE else FALSE<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60582 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11.jpg\" alt=\"Testing R Flat table objects\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/11-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>In the above command, the class of the object is not a &#8216;ftable&#8217; since a FALSE is returned.<\/p>\n<h3>R Summary Commands for Tables<\/h3>\n<p>A table is a way to summarize data and is often the end point of operation, for example, making a contingency table. However, it is desired to perform certain actions on a table itself.<\/p>\n<p>Some useful summary commands for tables are shown as follows:<\/p>\n<ul>\n<li><strong>table(x, margin = NULL, FUN) &#8211;<\/strong>\u00a0In order to obtain the various contents of the data frame, matrix or a table, we make use of this command. When specifying <em>margin as 1<\/em>, we obtain<em> row totals<\/em> and when the <em>margin is specified as 2<\/em>, we obtain the <em>column totals<\/em>.<\/li>\n<\/ul>\n<p>The <strong>prop.table()<\/strong> command is for displaying the proportions of the total sum. The index for rows and columns can also be added through which you can express your data in a better way by\u00a0various proportions of the rows and column sums. The prop.table() command can be used for displaying contents of the table. This is carried out in proportions of the total sum. Lastly, for expressing the data as proportions of various rows and column sums, you can also add an index.<\/p>\n<ul>\n<li><strong>addmargin(A, margin = c(1, 2), FUN = sum) &#8211;<\/strong>\u00a0For returning a function that is applied to the rows or columns of a table, we use the addmargin() command. With this command, you can use any function on the rows or columns.<\/li>\n<\/ul>\n<p>Essentially, you get a row of results. In most situations, you are going to use the function to produce summaries for both rows and columns.<\/p>\n<p><em><strong>Don&#8217;t forget to check the <a href=\"https:\/\/data-flair.training\/blogs\/r-recursive-function\/\">R Recursive Function<\/a><\/strong><\/em><\/p>\n<h3>Cross Tabulation in R<\/h3>\n<p>In order to represent the rows in a tabular format, we make use of the cross tabulation. For doing so, we make use of the xtabs() command as follows:<\/p>\n<p><em>xtabs(freq.data~categories.list, data)<\/em><\/p>\n<p>Notice that the sign tilde (~) is placed on the right-hand side of the frequency of the data.<\/p>\n<p>This is similar to the ftable() command which we discussed above. The logic is the same. That is, towards the left of tilde, we assign the name of the frequency of data and to its right, we assign the categories. These categories can be cross-tabulated with the plus(+) sign.<\/p>\n<p>Finally, we type the name of the data object towards the end and if we don&#8217;t, R will not find these variables!<\/p>\n<p>We can use this command on our &#8216;table1&#8217; data as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; table1.t = xtabs(A ~ B + C, data = table1)\r\n&gt; table1.t\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60589 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13.jpg\" alt=\"Cross-tabulation - R Contingency Tables\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/13-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>Testing Cross-Table (xtabs) Objects<\/h3>\n<p>When you use the <strong>xtabs()<\/strong> command, the object you create is a kind of table and gives a TRUE result using the <strong>is.table()<\/strong> command. It also gives a TRUE result if you use the <strong>as.matrix()<\/strong> command.<\/p>\n<p>As far as R is concerned, it holds two sorts of class. You can see this using the<strong> class()<\/strong> command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class(table1.t)<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60590 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1.jpg\" alt=\"Testing cross-table objects\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>If the xtabs object is to be tested, we may face an issue as the class result will possess two elements:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if(class(table1.t) == 'xtabs') TRUE else FALSE<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60591 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2.jpg\" alt=\"xtabs object test\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/14.2-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>This result is an error message because the &#8216;xtabs&#8217; result was produced first. There are several refinements to this which are required to pick out the desired values.<\/p>\n<h4>Recreating Original Data from a Contingency Table<\/h4>\n<p>The xtabs object can be reassembled into the data frame with the help of as.data.frame() command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; as.data.frame(table1.t)<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60592 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1.jpg\" alt=\"Recreating original data from contingency tables\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">as.data.frame(table1.t, responseName = 'B')<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60593 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2.jpg\" alt=\"Recreating original data from contingency tables 2\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/15.2-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>Switching Class in R<\/h3>\n<p>You can use the <strong>class()<\/strong> command to alter the class of an object and see the current class of the object. This can be useful at instances where an object needs to be in a certain class for a command to operate. In the following example, the bird object is queried and then reset using the class() command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; class(bird)\r\n&gt; attributes(bird)$class &lt;- \"table\"\r\n&gt; bird\r\n&gt; class(bird)\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60594 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1.jpg\" alt=\"Switching Class in R bird class\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>The matrix of bird observations is now classed as a table. You can now proceed to create a data frame from the table using the as.data.frame() command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; bird.df = as.data.frame(bird)\r\n<\/pre>\n<p>The columns are not labelled appropriately and the zero data is still intact in the result of the <strong>as.data.frame()<\/strong>\u00a0command. This can be modified by using <strong>names()<\/strong> command and reconstruct the data omitting the zero rows, shown as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; bird.tt = bird\r\n&gt; class(bird.tt) = 'table'  #DataFlair  \r\n&gt; bird.tt = as.data.frame(bird.tt)\r\n&gt; names(bird.tt) = c('Species', 'Habitat', 'Qty')\r\n&gt; bird.tt = bird.tt[which(bird.tt$Qty &gt; 0),]\r\n&gt; rownames(bird.tt) = as.numeric(1:length(rownames(bird.tt))) #dataflair\r\n&gt; bird.tt\r\n<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-60596 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3.jpg\" alt=\"Switching Class in R bird.tt\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/16.3-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<ul>\n<li>The first command simply creates a duplicate matrix to work on, keeping the original intact.<\/li>\n<li>The second command changes the class to \u201c<strong>table<\/strong>\u201d.<\/li>\n<li>The third command creates the data frame of original values.<\/li>\n<li>The fourth command alters the names of the columns.<\/li>\n<li>The penultimate command selects out the data that are greater than zero, effectively deleting 0 observations.<\/li>\n<li>The final command reinstates the row index labels to a continuous sequence.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this article, we studied contingency tables in R. This topic is one of the most important concepts and the mastery of it is utmost important to get an in-depth insight into R programming. Furthermore, learning how to manage data by building tables is an important procedure to ensure an efficient process of data analysis. We hope that you enjoyed reading this article.<\/p>\n<p><em><strong>Next article in your journey of R programming &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/r-graphical-models-tutorial\/\">R Graphical Models Tutorial<\/a><\/strong><\/em><\/p>\n<p>If you have any queries or feedback for us, share them in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This R tutorial is all about contingency tables in R. First of all, we will discuss the introduction to R contingency tables and different ways to create contingency tables in R.\u00a0 And, after completing&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":60951,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[11163,20321,11265,11271],"class_list":["post-2868","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-r-contingency-tables","tag-r-cross-tabulation","tag-r-summary-commands","tag-r-table-conversions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Introduction to Contingency Tables in R - A Vital Booster for Mastering R - DataFlair<\/title>\n<meta name=\"description\" content=\"R Contingency Tables tutorial covers its creation from vectors and data, conversion of R objects to Tables, R summary commands for tables and cross tabulation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to Contingency Tables in R - A Vital Booster for Mastering R - DataFlair\" \/>\n<meta property=\"og:description\" content=\"R Contingency Tables tutorial covers its creation from vectors and data, conversion of R objects to Tables, R summary commands for tables and cross tabulation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-16T03:24:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T12:50:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Contingency-Tables-in-R.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"808\" \/>\n\t<meta property=\"og:image:height\" content=\"432\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to Contingency Tables in R - A Vital Booster for Mastering R - DataFlair","description":"R Contingency Tables tutorial covers its creation from vectors and data, conversion of R objects to Tables, R summary commands for tables and cross tabulation.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to Contingency Tables in R - A Vital Booster for Mastering R - DataFlair","og_description":"R Contingency Tables tutorial covers its creation from vectors and data, conversion of R objects to Tables, R summary commands for tables and cross tabulation.","og_url":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-06-16T03:24:55+00:00","article_modified_time":"2021-08-25T12:50:55+00:00","og_image":[{"width":808,"height":432,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Contingency-Tables-in-R.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Introduction to Contingency Tables in R &#8211; A Vital Booster for Mastering R","datePublished":"2017-06-16T03:24:55+00:00","dateModified":"2021-08-25T12:50:55+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/"},"wordCount":2417,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Contingency-Tables-in-R.jpg","keywords":["R contingency tables","R Cross Tabulation","R summary commands","R table Conversions"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/","url":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/","name":"Introduction to Contingency Tables in R - A Vital Booster for Mastering R - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Contingency-Tables-in-R.jpg","datePublished":"2017-06-16T03:24:55+00:00","dateModified":"2021-08-25T12:50:55+00:00","description":"R Contingency Tables tutorial covers its creation from vectors and data, conversion of R objects to Tables, R summary commands for tables and cross tabulation.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/r-contingency-tables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Contingency-Tables-in-R.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/06\/Contingency-Tables-in-R.jpg","width":808,"height":432,"caption":"Contingency Tables in R"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/r-contingency-tables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"R Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/r\/"},{"@type":"ListItem","position":3,"name":"Introduction to Contingency Tables in R &#8211; A Vital Booster for Mastering R"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/2868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=2868"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/2868\/revisions"}],"predecessor-version":[{"id":61068,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/2868\/revisions\/61068"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/60951"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=2868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=2868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=2868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}