## Introduction

This article describes how to use common mathematical functions in Displayr via R.

## Requirements

Mathematical formulas can be used within an R variable, calculation or data set. Variable sets, tables and data sets are all effectively tabular data. The main difference is whether the underlying data is raw data or aggregated data. Raw data will be made up of rows that represent each record and columns that represent each variable. Aggregated data, on the other hand, will be made up of rows and/or columns that represent different intersecting categories.

### Tables

In this example, we will use a table of counts called *table.Brand.Image*:

To make it more straightforward, we have removed all NETs and will use all the remaining table cells in our calculations. If you wish to, for example, ignore the **None of these** row, you can first define the table and then filter it when running the function:

t = table.Brand.Imagefunction_name(t[rownames(t) != "None of these",])

Remember, by default NET (or SUM, if numeric) categories will be added to your table. If you wish to exclude these from your calculation, you will need to either remove them from your table via right-click **> Hide**, or else add this exclusion in your code.

### Variable sets

In this case, we could easily substitute our table with the *Brand Image* variable set itself which is made up of 63 binary variables (9 statements by 7 brand categories):

When dealing with variable sets, you can either reference each variable by **Name** or the entire variable set by **Label** (found under **Properties > GENERAL**):

function_name(cbind(q5a1,q5a2,q5a3,q5a4,q5a5,q5a6,q5a7,q5b1,q5b2,q5b3,q5b4,q5b5,q5b6,q5b7,q5c1,q5c2,q5c3,q5c4,q5c5,q5c6,q5c7,q5d1,q5d2,q5d3,q5d4,q5d5,q5d6,q5d7,q5e1,q5e2,q5e3,q5e4,q5e5,q5e6,q5e7,q5f1,q5f2,q5f3,q5f4,q5f5,q5f6,q5f7,q5g1,q5g2,q5g3,q5g4,q5g5,q5g6,q5g7,q5h1,q5h2,q5h3,q5h4,q5h5,q5h6,q5h7,q5i1,q5i2,q5i3,q5i4,q5i5,q5i6,q5i7))

function_name(`Q5 - Brand Image`)

In this example, using the **Label** method is much more efficient than referencing all 63 variables by name!

By default, NET (or SUM, if numeric) categories will likewise be added to your variable set when referencing by **Label**. However, if you hide these categories in the corresponding table of this variable set, they will also be removed from the variable set. Otherwise you will need to remove them in your code.

## Method

## Base R functions

The following are built-in R functions that will apply to all rows or columns. We have used the *na.rm=T* argument here to deal with missing values. If this argument is not used and the function encounters missing values, it will simply return NA, which is why including this is a good habit.

### 1. Sum each row over the columns:

rowSums(table.Brand.Image, na.rm=T)

### 2. Sum each column over the rows:

colSums(table.Brand.Image, na.rm=T)

### 3. Average each row over the columns:

rowMeans(table.Brand.Image, na.rm=T)

### 4. Average each column over the rows:

colMeans(table.Brand.Image, na.rm=T)

*Apply* functions

Some functions can only be run on all rows or columns within an *apply* or similar function. The format is:

apply(data, rows or columns, function)

The *rows or columns* argument requires as **1** for **rows** and **2** for** columns**.

### 1. Minimum of each row over the columns:

apply(table.Brand.Image, 1, min)

### 2. Minimum of each column over the rows:

apply(table.Brand.Image, 2, min)

### 3. Maximum of each row over the columns:

apply(table.Brand.Image, 1, max)

### 4. Maximum of each column over the rows:

apply(table.Brand.Image, 2, max)

## Custom *apply* functions

The final parameter of the* apply* function allows for custom functions. The format is:

function(x) {xdo something}

The curly brackets are optional for basic examples but become necessary when you have more elaborate multi-line functions.

### 1. Any rows over 50:

apply(table.Brand.Image, 1, function(x) any(x>50))

### 2. All rows over 20:

apply(table.Brand.Image, 1, function(x) all(x>20))

## See Also

How to Work with R in Displayr

How to Work with Conditional R Formulas

## Comments

0 comments

Article is closed for comments.