This article contains a series of examples on how to quickly create custom R calculations by incorporating Displayr's point-and-click functionality. These examples are simple in nature, but the techniques in creating them can be applied to more complex *Calculations*.

For more details, check out the Calculate Anything post in our What Displayr Can Do For You (the Magic) section. The post includes other examples of built-in calculations offering a nice overview of the different ways to create calculations. While the Bespoke Analyses article explains the more standard options in the **Calculation** menu that you can create using point-and-click options and the *Inputs* menu. These *Calculations* still use R on the back end.

## Requirements

A Displayr document with a dataset imported

## Method - Calculation between two columns in a table

For our example, we will be using the brand attitude table below. It was created by dragging the *Brand attitude 2* variable set onto a blank page. We will create a calculation to subtract the *Detractors* column from the *Promoters* column, resulting in an NPS score for each row.

- Click the
**Calculation = sign**. - Draw a box for the Calculation on the page.
- While the cursor is blinking in the box, hold down the Ctrl key and click on the
**Promoters**column header. - Hit the
**F4**key to change the number of the column to the column header - just to be sure we are always referencing the*Promoters*column. - Type the subtraction sign
`-`

. - Repeat steps 4 and 5 for the
**Detractors**column. Your final code should look similar to:

#subtract the promoters column from the detractors column

table.Brand.attitude.2[,"Promoters"] - table.Brand.attitude.2[,"Detractors"]

7. Click **Calculate**.

8. [OPTIONAL] You can use the **APPEARANCE** section in the **object inspector** to further customize the final look of your output. The final output will look similar to:

## Method - Calculation using variables in the dataset

In this example we are going to use our *q2* variable set to create a variable to identify "Heavy Coke Drinkers" - we wish to include respondents who drink more than 7 drinks per week. Here is a preview of how the variable set is structured:

- Hover over q2 in the
**Data Sets**tree and click on the**+ sign > Custom Code > R - Numeric**. - In the
**object inspector**, give it a**Label,**eg*Heavy Coke Drinkers.* - [OPTIONAL] Check
**Usable as a filter,**if wanting to filter outputs by this variable. - In the
**Data Sets**tree, expand the**q2**variable set to see the individual variables. - Drag
**Coca-Cola - When 'out and about'**variable to the**R CODE**box on the right. - Type a plus sign
`+`

. - From the
**Data Sets**tree, drag**Coca-Cola - When 'at home'**to the right of the plus sign in**R CODE**. - Set this calculation to an intermittent variable called totalcoke and create a condition to see which respondents drank more than 7 cokes a week on average. So your final code should be as follows:

`#add two coke drink variables together`

totalcoke = q2a_1+q2b_1

#create a filter to filter in those with more than 7 drinks of coke per week

totalcoke > 7 - Click
**Calculate**.

See the below gif for a demo and to see a preview of the final results from the variable after Calculating.

## Method - Calculation using two tables

In this example we will use two tables for the variable sets: 1) *Preferred cola* and 2) *Brand attitude 2*.

We will create a new Calculation to see how many promoters of each cola did not indicate that cola was their preferred brand. However, notice the rows in *Preferred cola* are in a different order and have extra categories than our Brand attitude 2 table. This would be an issue if simply subtracting the columns using R so we will use the Subtract() function from the verbs R package.

- Click the
**Calculation = sign**. - Draw a box for the Calculation on the page.
- Type in Sub and notice options for functions start to appear, click on the first one for
**Subtract**. This Subtract function is from our verbs R package and can take various inputs and automatically ignores missing data - more info here. - With the cursor inside the parentheses, click on the column for
**Promoters**. - Type a comma
**,**. - Click on the column in the
**Preferred cola SUMMARY**table. Your final code should be:

#subtract column in table from other table

Subtract(table.Brand.attitude.3[,3],table.Preferred.cola) - Click
**Calculate**.

Your final table will include only the rows found in each table with the appropriate numbers subtracted.

If you'd like to customize which rows are shown and other arguments, you can see information on this function by typing help(Subtract) as the last line of your R CODE.

## Creating R code to Select Cells in a Table

To select cells in a table with R code, do the following. Notice that the R code appears in the RCODE windows to the right of the table. Also, the cells I select are highlighted just as they are in Excel:

Use the F4 key If you prefer to see the labels rather than the numeric values for the rows or columns. For example:

## Recap

- Clicking on bits of tables while editing R code will insert a reference to it in the code. To see a preview of the data you are referencing, hover over the name in the
**R CODE**box. - When referencing specific parts of a table, you can use F4 to toggle between absolute references, by label, and relative reference, by number.
- When you begin typing the name of a function, variable, or output in your code, a list will appear below from which you can select the name - as a shortcut to typing.
- Some examples above utilize functions in the verbs R package, created by our team here at Displayr. Using functions from this package vs others incorporates an amount of data matching and reformatting necessary to account for data inconsistencies commonly seen in real-world data sets.

## Next

How R Works Differently in Displayr Compared to Other Programs

How to Reference and Distinguish between Different R Objects in Displayr

How to Use Different Types of Data in R

How to Perform Mathematical Calculations Using R

How to Extract Data from a Multiple Column Table

How to Create a New Variable Based on Other Variables using R