<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-do-simple-table-manipulations-with-r-using-displayr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/how-to-do-simple-table-manipulations-with-r-using-displayr/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 780px) 100vw, 780px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2017/04/Using_Displayr.png?w=450&ssl=1" alt="Using Displayr" class="wp-image-1473" data-recalc-dims="1"></p>
<p>R is not just a tool for the data science elite. It is an immensely powerful tool that can be used by market researchers in everyday analysis. (Displayr helps make this possible by providing an easy to use interface that is integrated with your document and data). In this post, we’re going to focus on how you can use some simple R to manipulate tables. Specifically, we’ll look how R can be used to compute new rows and columns. We’ll look at how this is done through worked examples. The intention, of course, is for you to be able to take away the principles of the code, so that you can adapt and apply it flexibly and easily in your document.</p>
<p>The computations we’re going to look in this post include:</p>
<ul>
<li>The difference between 2 columns (or rows)</li>
<li>Creating a custom index score (involving multiple categories in a table(s))</li>
<li>The average across a series of statistics (eg: the average of a row or column in a table)</li>
<li>Inserting any of above back into your original table (ie: inserting a new row or column)</li>
<li>Calculations involving entire tables (not just rows or columns, but the table in its entirety)</li>
<li>How to rename columns and rows of an existing R table</li>
</ul>
<hr>
<h2>A recap on some key concepts</h2>
<p>I recommend you read the post on <a rel="nofollow" href="https://www.displayr.com/how-to-build-a-brand-funnel-in-displayr-using-r/" target="_blank">How to build a Brand Funnel in Displayr using R</a> because I cover off some basics of R.</p>
<p>As a refresh, there are some key concepts for <a rel="nofollow" href="https://docs.displayr.com/wiki/Category:R" target="_blank">using R in Displayr</a>:</p>
<ul>
<li><strong><em>Names</em> </strong>– this is the name that Displayr uses in R code to refer to another output (eg: a table). Once you select an object, you can go to the <a rel="nofollow" href="https://docs.displayr.com/wiki/Object_Inspector" target="_blank">Object Inspector</a> > <span>Properties</span> > <span><strong>GENERAL </strong></span>> <strong>Name </strong>and you can see the current name of that object. Here, you can easily change the name to anything you like (something perhaps easier to remember and type)</li>
<li><em><strong>Subscripting</strong> </em>– this is how we reference cells within a source table in R. We put either a <em>number</em> or some “<em>text</em>” in <span>[square brackets]</span>. If there are two dimensions to the source table (rows and columns) we use items separated by a comma <span>[rows, columns]</span>. But as we explore in  <a rel="nofollow" href="https://www.displayr.com/how-to-build-a-brand-funnel-in-displayr-using-r/" target="_blank">How to build a Brand Funnel in Displayr using R</a>, with a two-dimensional table, you can reference an entire slab (rectangular slice of the table) by leaving either the row or column item reference blank. That is, a blank before or after the comma.</li>
<li><span> Calculate <span> </span></span>and <strong>Automatic </strong>calculations – After you write or change any <span><strong>R CODE</strong></span> in the <a rel="nofollow" href="https://docs.displayr.com/wiki/Object_Inspector" target="_blank">Object Inspector</a>, you need to either click <span> Calculate </span> or check the <strong>Automatic</strong> box. I recommend <span>not</span> ticking Automatic until you’ve finished with all your coding. That way it’s not going to keep re-updating every time you touch a line of code. You want it to be automatic <em>after</em> you’ve finished, so your R output always stays up to date.</li>
<li><span>Blue reference highlighting </span>– if Displayr is successfully able to identify a reference name of a table or another R output in the report, it will <span>highlight in blue</span>. If it’s not highlighted in blue, it’s not referencing something outside that coding window. If you hover your mouse over anything that highlights in blue like this, it should pop up a representation of the object being referred to (for convenience).</li>
<li>Functions in R are Case SeNSitIVe – so there’s no particular logic why <code>rownames()</code> and <code>rowMeans()</code> as functions include or don’t include a capital (most capitalize the second word and so on within the function name). You just need to make sure you get it right!</li>
</ul>
<p>In the image below, I show the label of an object selected (<em>Preferred Cola</em> by <em>Age)</em>. I rename this <span>pref_tab <span>in the subsequent worked example so it’s easier to remember/type.</span></span></p>
<p><img sizes="(max-width: 893px) 100vw, 893px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/09/SetName.png?w=450&ssl=1" alt="Setting name in object inspector" class="wp-image-10558" data-recalc-dims="1"></p>
<hr>
<h2>Calculating the difference between two columns</h2>
<p>When you want to calculate the difference between two numbers, you minus one from the other. That seems pretty obvious (4 – 3 = 1). But likewise, you can subtract one variable from another to produce a new variable (eg: x – y = z). The variable z will therefore change as x and y change. In R, the same principle can apply to rows and columns of tables (and actually to entire tables, as we’ll discuss at the end). We can let the row or column act as variable. In R terms, this is called a <em>vector</em>.</p>
<p>Let’s show this via example on the table above (<em>Preferred Cola by Gender,</em> which has a name of<span> pref_tab</span>).</p>
<p><img sizes="(max-width: 330px) 100vw, 330px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/09/Pref_cola_gender-1.png?resize=330%2C228&ssl=1" alt="Pref Cola Gender" width="330" class="wp-image-10561" data-recalc-dims="1" height="228"></p>
<p>We could consider the entire column <em>Female</em> to be <span>y</span>. So the first line code in an R Output (<strong>Insert > R Output</strong>) would be: <code>y = pref_tab[,&quot;Female&quot;]</code></p>
<p><img sizes="(max-width: 212px) 100vw, 212px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/02/Capture.png?resize=212%2C235&ssl=1" alt="Cola Preference - Female array" width="212" class="wp-image-4583" data-recalc-dims="1" height="235"></p>
<p>And then Male column, on the second line of code, we could store as: <code>x = pref_tab[,&quot;Male&quot;].</code>So, therefore, the difference between <span>y</span> and <span>x</span> is simply <code><span>y – x</span></code> (that’s my third line). I could just leave it at that, but I’m going to do a bit of further housekeeping on the output. I’m going to store it as a vector called <code>z</code> (so I can use <code>z</code> later in other computations, if I choose). So I’m going to change my third line to be <code><span>z = y – x</span></code>. Second, in the <strong><a rel="nofollow" href="https://docs.displayr.com/wiki/Ribbon" target="_blank">Ribbon</a> > Appearance</strong>, I’m going to remove the decimal places and apply a percentage sign. The final result is then the below:</p>
<p><img sizes="(max-width: 220px) 100vw, 220px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/02/CaptureA.png?resize=220%2C231&ssl=1" alt="Cola Preference - Gender Differences" width="220" class="wp-image-4584" data-recalc-dims="1" height="231"></p>
<p>And there you have it – the difference between Female and Male for all the brands. I didn’t have to calculate the brands separately, it did it all just at once, because I was referencing columns that were side-by-side in a table (and so I know all the order of the brands align in the vectors <code>x</code> and <code>y</code>). The resultant <code>z</code> variable keeps names of each item in the vector.</p>
<hr>
<h2>Creating a custom Index score across multiple categories</h2>
<p>Researchers like to use different metrics in the course of their work. Sometimes these metrics are indexes that they have to replicate, or they make up themselves. These go beyond the simple adding or subtracting of percentages (as in the above), as in the creation of custom index scores. The computations behind these scores can be whatever you like. The beauty of a tool like R is that it allows you to pick up numbers from your table, play with them (ie: do computations with them), and then put them back together. And once you’ve set it up, it should just update automatically when new table comes in, or if you modify something in the source tables.</p>
<p>Again, I’ll show how you can do this via example. Bear in mind that the below index calculation is something I’ve made up. The purpose of the worked example is to demonstrate the logic behind the R code so that you can adapt it to whatever scenario you’re in. In the example, we’ll use the following table, which is a summary table of an attitudinal scale question. Respondents in a survey were asked to rate their attitude towards 6 different cola brands on a scale from Hate to Love. In the following example I’ve given it a name <code><span>brand_att</span></code> for convenience.</p>
<p><img sizes="(max-width: 525px) 100vw, 525px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/09/BrandAttitude.png?w=450&ssl=1" alt="Brand Attitude" class="wp-image-10562" data-recalc-dims="1"></p>
<p>Let’s say I need to create a special index, based on the following formula. It is basically the top 2 box score, divided by the sum of the non-neutral scores, for each brand. So for Coca-Cola this index would be (42 + 32) / <sup>1</sup><span>42 + 32) + (7 + 4</span> * 100 = 87.</p>
<p><img sizes="(max-width: 653px) 100vw, 653px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2017/12/formula.png?w=450&ssl=1" alt="formula" class="wp-image-4328" data-recalc-dims="1"></p>
<p>My first line of code would be to calculate the top 2 box: <code>t2b = brand_att[,&quot;Like&quot;] + brand_att[,&quot;Love&quot;].</code> Notice that I’m storing my top 2 box array as a variable called <code>t2b</code>. The variable isn’t a single number, but rather 6 consecutive numbers, each tagged with the brand name in the same order as the source table (<em>Coca-Cola</em> thru <em>Pepsi Max</em>). The next line would be the bottom 2 box: <code>t2b = brand_att[,&quot;Hate&quot;] + brand_att[,&quot;Dislike&quot;].</code></p>
<p>And then I can work my index out using basic algebra on my variables. I could have squashed lines 3 through 5 in the below into the one line, but I’ve split it out just for the sake of illustration and made new variables <code>numerator</code> and <code>denominator</code>.</p>
<pre>

t2b = brand_att[,&quot;Like&quot;] + brand_att[,&quot;Love&quot;]
b2b = brand_att[,&quot;Hate&quot;] + brand_att[,&quot;Dislike&quot;]
numerator = t2b
demoninator = t2b + b2b
index = (numerator / demoninator) * 100

</pre>
<p><img sizes="(max-width: 188px) 100vw, 188px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/02/Capture-1.png?resize=188%2C170&ssl=1" alt="Brand index result tables" width="188" class="wp-image-4585" data-recalc-dims="1" height="170"></p>
<hr>
<h2>The average across a series of statistics</h2>
<p>Sometimes we want to compute the <em>average of statistics</em> in a table<span>. This is different to computing the <em>average of variables</em>. The latter is computed at the respondent-level (each respondent gets a <a rel="nofollow" href="https://docs.displayr.com/wiki/Value_Attributes" target="_blank"><em>value</em></a>). You can use <a rel="nofollow" href="https://docs.displayr.com/wiki/Category:R#R_Variables" target="_blank">R variables</a> to do that, or make a <a rel="nofollow" href="https://docs.displayr.com/wiki/JavaScript" target="_blank">JavaScript variable.</a> By contrast, we are generating the <strong>average of statistics</strong> in an R Output, working with the outputs from other tables.  </span></p>
<p>To do this, R actually has a function for this purpose ready to go. It saves us having to specify the arithmetic as I did in my worked example (11.3%) a moment ago. To use a function in R, you write the function name and then enclose in rounded brackets the bits you want the function to act on (called the <em>arguments </em>in R terminology). In this case, we have two useful functions: <code>colMeans()</code> and <code>rowMeans()</code>. I put the () brackets after them to reinforce the point they are functions (and need stuff to go inside their brackets). To see how this could apply to the Brand Attitude table above, you could do the following (note: <code>rowMeans()</code> wouldn’t make much sense in this example!).</p>
<p><img sizes="(max-width: 309px) 100vw, 309px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2017/12/brand-att.png?resize=309%2C227&ssl=1" alt="brand attitude" width="309" class="wp-image-4331" data-recalc-dims="1" height="227"></p>
<p>Let’s consider how it can apply to another table. Consider this Pick Any – Grid question called <em>Brand Image.</em> Respondents could freely click as many of the cells below in the survey.</p>
<p><img sizes="(max-width: 830px) 100vw, 830px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/09/BrandImage.png?w=450&ssl=1" alt="Brand Image" class="wp-image-10563" data-recalc-dims="1"></p>
<p>I can use <code>colMeans()</code>to create an average across all the brands for each attribute: <code>attribute_average = colMeans(brand_image).</code></p>
<p><img sizes="(max-width: 370px) 100vw, 370px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2017/12/Attribute-average.png?resize=370%2C311&ssl=1" alt="attribute average" width="370" class="wp-image-4329" data-recalc-dims="1" height="311"></p>
<p>Simple right?</p>
<p>Wrong. Well, it’s sort of right. It’s right in the sense that 34 is the literal average of all the numbers in the <em>Feminine</em> column. But this is including the <em>None of these </em>and even the <em>NET</em> categories, which you are likely to not want to include in the computation. So how do you exclude those two categories? You could change your source table (ie: select the <em>None of these</em> and the <em>NET</em> categories, and then from the <strong>Ribbon > Data Manipulation > Delete (Rows/Columns)</strong>). That would change how the question is represented everywhere in the document, so you may like to duplicate the variable (set) first (select the variable set in the <a rel="nofollow" href="https://docs.displayr.com/wiki/Data_Tree" target="_blank">Data tree</a> and then <strong>Home > Duplicate</strong>), and alter the duplicate.</p>
<p>The other way would be to subscript your source table in the R code, so that we’re only extracting the rows and columns we want. To do that, I could specify which brands I want (and in which order) by using an <em>array. </em>We make the array by using the combine function <code>c().</code> (See <a rel="nofollow" href="https://www.displayr.com/how-to-build-a-brand-funnel-in-displayr-using-r/" target="_blank">How to Build a Brand Funnel in Displayr using R</a> for an in-depth discussion of referencing rows and columns arrays of brand labels). I would use the <code>brand</code> array to the <code>colMeans()</code> and the attributes. When it comes to the <code>rowMeans()</code>, I don’t want to include the NET column (which is 100 for each row). So I would similarly do the same by making an <code>attributes</code> array to use when referencing the table. Although specifying the labels seems a little tedious, it’s a case of <em>a stitch in time</em>: you are not at risk of accidentally referencing the wrong rows.</p>
<pre></pre>
<pre>

brands = c(&quot;Coca-Cola&quot;,&quot;Diet Coke&quot;,&quot;Coke Zero&quot;,&quot;Pepsi&quot;,&quot;Diet Pepsi&quot;,&quot;Pepsi Max&quot;)
attribute_average = colMeans(brand_image[brands,])

attributes = c(&quot;Feminine&quot;,&quot;Health-conscious&quot;,&quot;Innocent&quot;,&quot;Older&quot;,&quot;Open to new experiences&quot;,&quot;Rebellious&quot;,&quot;Sleepy&quot;,&quot;Traditional&quot;,&quot;Weight-conscious&quot;)
brand_average = rowMeans(brand_image[,attributes])

</pre>
<hr>
<h2>Inserting your computation back into your original table</h2>
<p>In all the outcomes above, we can see a new array of information (mostly by brands, but sometimes by attributes as in the last example). We’ve got the numbers, but you may like to integrate this back into your original table. Perhaps just to give it some context, or perhaps you want to feed the overall table into a visualization or report or whatever.</p>
<p>It’s quite easy to do, and just involves adding a couple more lines of code to everything you’ve done this far. R has functions called <code>rbind()</code> and <code>cbind()</code>, which are <em>row-bind</em> and <em>column-bind</em> respectively. But at Displayr (&amp; Q) we have better functions (we believe) called <code>Rbind()</code> and <code>Cbind()</code> respectively (with capital first letters). These are part of our <a rel="nofollow" href="https://github.com/Displayr/flipTables/blob/master/R/r_and_c_bind.R" target="_blank"><code>flipTables</code></a> package. To use  these functions in Displayr, add the line <code>library(flipTables)</code> upfront, as I do below. The reason why these functions are better than the <a rel="nofollow" href="https://docs.displayr.com/wiki/Standard_R" target="_blank">Standard R</a> is that they can match row and column labels. This may alleviate the need to create brand arrays when combining tables, or at least act as an additional safeguard to ensure things align properly.</p>
<p>We’re going to use these functions to “bind” the row and column mean arrays to the original table. Let’s take our first example, the difference between the genders on their main cola. There’s a couple of ways we can include the new array that represents the differences in the original table. One way is to tell R to combine the original table with the new array column-wise with the 5th row of code below.</p>
<pre>
library(flipTables)
y = pref_tab[,&quot;Female&quot;]
x = pref_tab[,&quot;Male&quot;]
z = y - x
table = Cbind(pref_tab,&quot;Difference&quot; = z)
table[,c(1,2,4)]
</pre>
<p>In the above, I’m specifying the label for the column. I just put the desired label in speech marks “” and let it equal the variable: <code>&quot;Difference&quot; = z</code></p>
<p>The NET is a bit annoying, and I may not want to see it. One way to remove it is to remove it or hide it from the source table <code>pref_tab</code>. Another way, is just to tell the R code that we only want the 1st, 2nd and 4th columns:<code> table[,c(1,2,4)]</code></p>
<p><img sizes="(max-width: 422px) 100vw, 422px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2018/01/CaptureABCD.png?resize=422%2C229&ssl=1" alt="Table with Male, Female and Difference" width="422" class="wp-image-4466" data-recalc-dims="1" height="229"></p>
<p>There’s also another way to achieve the above as well. Given I’ve got x, y and z set up as variables, I could column-bind all three, and give them labels at the same time. I’ve purposively changed the order of the columns to prove the point!  A new arrangement in the table is flexible and at your control!</p>
<pre>

y = pref_tab[,&quot;Female&quot;]
x = pref_tab[,&quot;Male&quot;]
z = y - x
table = cbind(&quot;Difference&quot; = z,&quot;Female&quot; = y,&quot;Male&quot; = x)

</pre>
<p><img sizes="(max-width: 440px) 100vw, 440px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2018/02/Capture-2.png?resize=440%2C233&ssl=1" alt="Recombination table" width="440" class="wp-image-4586" data-recalc-dims="1" height="233"></p>
<p>On our brand attitude table in the post above, we could use <code>rbind()</code> to place the average % across brands as a row at the top (plus give it a label). It’s just:</p>
<pre>

a = colMeans(brand_att)
table = rbind(&quot;Average&quot; = a,brand_att)

</pre>
<p><img sizes="(max-width: 616px) 100vw, 616px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/02/Capture-3.png?w=450&ssl=1" alt="rbind average table" class="wp-image-4587" data-recalc-dims="1"></p>
<p>Finally, if you wanted to add the row and column averages to the Brand Image table, you can apply both <code>rbind()</code> and <code>cbind()</code> to the one table. Noting, of course, that you need to use <code>rbind()</code> and <code>cbind()</code> the right-way around!</p>
<pre>

attribute_average = colMeans(brand_image[1:6,])
brand_average = rowMeans(brand_image[,1:9])
table = rbind(brand_image[1:6,],&quot;Average&quot; = attribute_average,&quot;None of these&quot; = brand_image[&quot;None of these&quot;,])
cbind(table[,1:9],&quot;Average&quot; = brand_average)

</pre>
<p><img sizes="(max-width: 1236px) 100vw, 1236px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/02/Capture-4.png?w=450&ssl=1" alt="Table recombined" class="wp-image-4588" data-recalc-dims="1"></p>
<p>A word of caution: Using <code>rbind()</code> and <code>cbind()</code> won’t match your category names (eg: brands). So if the thing you are row-binding or column-binding has a different order of brands, or mismatching category labels, you may be better exploring other functions, such as the <code>MergeTables()</code> or <code><a rel="nofollow" href="http://wiki.q-researchsoftware.com/wiki/Tables_-_Merge_Two_Tables" target="_blank">Merge2Tables()</a></code> (shortcut available via the <b>Home > Utilities > Tables</b> menu in the Ribbon). In this case, my 4th line of code above is leading to an error, which I’ve highlighted in yellow in the image above. The 15.8 is actually the average for “None of these”, so it doesn’t match up! In this particular example, I would have been better to just recompute the <code>rowMeans()</code> again, so I’ve done that in the version below. Much better!</p>
<pre>

attribute_average = colMeans(brand_image[1:6,])
brand_average = rowMeans(brand_image[,1:9])
table = rbind(brand_image[1:6,],&quot;Average&quot; = attribute_average,&quot;None of these&quot; = brand_image[&quot;None of these&quot;,])
cbind(table[,1:9],&quot;Average&quot; = rowMeans(table))

</pre>
<p> </p>
<hr>
<h2>Calculation involving entire tables</h2>
<p>I just want to briefly mention that R is smart enough to do calculations that match up entire tables, not just rows and columns. You can do computations on entire slabs (rectangles) of information. In R lingo, a rectangular slab of information is treated as a matrix.</p>
<p>Here’s a simple example. Suppose I had my original <em>Brand Image</em> table above, but I had two versions of it. One table filtered by males. The second other filtered by females. I give each of these reference names of <code><span>brand_image_male</span></code> and <code><span>brand_image_female</span></code> respectively. Then, I can simply subtract one from the other to get the difference. Yes, it’s that easy.</p>
<p><img sizes="(max-width: 876px) 100vw, 876px" src="https://i0.wp.com/www.displayr.com/wp-content/uploads/2017/12/differen.png?w=450&ssl=1" alt="difference" class="wp-image-4350" data-recalc-dims="1"></p>
<p>This could make for a delicious visualization, so why not try hooking this last output up to a visualization? I’ve done that in the <a rel="nofollow" href="http://wiki.q-researchsoftware.com/images/9/9d/Table_computations_with_R.QPack" target="_blank">example QPack</a> (which you can <a rel="nofollow" href="https://www.displayr.com/using-q-projects-in-displayr/" target="_blank">upload into Displayr</a> to try it out).</p>
<hr>
<h2>How to rename columns and rows of an existing R table</h2>
<p>Tables made with R are different to those that you can make by dragging items from the Data Tree.  See the section “Creating an R Output” <a rel="nofollow" href="https://www.displayr.com/how-to-build-a-brand-funnel-in-displayr-using-r/" target="_blank">in this post</a> for more detail on how R tables are different. Inconveniently, you can’t just right-click on a category in an R table to rename it. You need to use a bit of code in order to rename the columns or rows. In the post above, we mentioned how you can set the row or column labels at the time you are using <code>rbind()</code> or <code>cbind()</code>. But you can also change the row and column labels on a completed table using the functions <code>rownames()</code> and <code>colnames()</code>. Here’s how to do it.</p>
<p>Take the Brand Image table from above where I calculated the difference between the male and female scores in the grid. Say I wanted to change the label <em>Coke</em> to <em>Coca-Cola.  </em>I could do that using <code>rownames()</code> and specifying the new row names using the combine function <code>c()</code>. Note: on the third line of code, I had to repeat <code>table</code> to return the table as the output, otherwise it would just return the string of brand names I’ve specified on the second line.</p>
<pre>

table = brand_image_male - brand_image_female
rownames(table) = c(&quot;Coca-Cola&quot;,&quot;Diet Coke&quot;,&quot;Coke Zero&quot;,&quot;Pepsi&quot;,&quot;Diet Pepsi&quot;,&quot;Pepsi Max&quot;,&quot;None of these&quot;,&quot;NET&quot;)
table

</pre>
<p><img sizes="(max-width: 842px) 100vw, 842px" src="https://i2.wp.com/www.displayr.com/wp-content/uploads/2018/02/Capture-6.png?w=450&ssl=1" alt="Relabelled table" class="wp-image-4590" data-recalc-dims="1"></p>
<p>But that was a bit of a bore having to write out all the row names again. Imagine if there were lots and lots of rows! And, I could easily have made a mistake. I could have swapped Diet Pepsi and Pepsi Max around, and hence made a mislabelling error. To get around this, I could alter the code so that I’m just renaming the Coke bit, and nothing else:</p>
<pre>

table = brand_image_male - brand_image_female
rownames(table)[rownames(table) == &quot;Coke&quot;] = &quot;Coca-Cola&quot;
table

</pre>
<hr>
<h2>Conclusion:</h2>
<p>Here I have discussed just some applications of R. Really, you are only limited by your imagination as to what you can do with your data. We invite your feedback and questions about the content. Do let us know if there are other ways we can help you use Displayr better.</p>
<p>I invite you to see the code in action. You can download the <a rel="nofollow" href="http://wiki.q-researchsoftware.com/images/9/9d/Table_computations_with_R.QPack" target="_blank">QPack from here</a> (which you can easily <a rel="nofollow" href="https://www.displayr.com/using-q-projects-in-displayr/" target="_blank">upload into Displayr</a> to try it out).</p>
<div>
<p><span>References</span><span>   [ <a>+</a> ]</span></p>
</div>
<div>
<table>
<tbody>
<tr>
<td><span>1.</span></td>
<td><span>↑</span></td>
<td>42 + 32) + (7 + 4</td>
</tr>
</tbody>
</table>
</div>
<p></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.displayr.com/how-to-do-simple-table-manipulations-with-r-using-displayr/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/B05BQ9eAraQ" height="1"></a></div>