## Introduction

This article describes useful methods for troubleshooting custom R code.

## Requirements

An R variable, calculation or data set.

## Method

### 1. Tips

- If adding data using R or creating an R variable, try the code out in a Calculation first to make sure it returns what you expect.
- Check the output as you are in the process of writing code to be sure your code is doing what you expect.
- Use # in your R code to add comments so it's easy to see what each section of code does:
`x = 1 # this is a comment`

- In many instances it's useful to simply look at some of your variables/tables in the code to spot unexpected inputs or results.
- R is case-sensitive so make sure you have matched the case in your referencing.
- When working with Calculations, check
**Properties > OUTPUT > Show raw R output**to see which exact line in your code is causing the error and to display intermediate outputs in your code. - Many errors and warnings are pretty generic and abstract sounding, but you can google them to see examples from other people who are receiving this error to tip you off to what may be the issue with your code.

### 2. Helpful Functions

There are a number of functions which can be used to help work out where an issue in your code exists.

In this example I have defined my table as *x *and have ticked **Show raw R output**:

`x = table.Preferred.cola`

**head()**- a quick way to inspect your table/variable in your code to make sure it's setup as you expect.`head(x)`

will display the top 6 rows by default and`head(x,10)`

will display the top 10.**str()**- describes the data and structure of the variable/calculation.`str(x)`

will return the structure of your data, including names, statistics and data types.**class()**- tells you what R class property your table/variable belongs to.`class(x)`

in this example will display*matrix*and*array*.**length()**or**dim()**- tells you the size of your variable.`length(x)`

in this case will return 18 (the total number of table cells) and`dim(x)`

will return 9 and 2 to represent the number of rows and columns respectively.**setdiff()**- shows you items in one list that are not in the other.`setdiff(rownames(x),c("Coca-Cola","Diet Coke","Coke Zero"))`

will compare the table row labels to these three Coke brands and return only the Pepsi and 'unengaged' categories.**sum(**- see how many times a condition holds*condition*)*true*using sum to make sure your condition/logical test is setup correctly.`sum(x[,"Count"] > 20)`

will return a*true*if the sum of the**Count**column is over 20.**any(**or*condition*)**all(**- check to see if any or all of the items you're testing come back*condition*)*true*.`any(x[,"Count"] < 20)`

will tell you if any of your**Count**rows are less than 20, and`all(x[,"Count"] > 0)`

will tell you if all your**Count**rows are greater than 0.

### 3. Common Errors

**Incorrect number of dimensions / subscript out of bounds**

Example:

The below code will produce an *incorrect number of dimensions* error due to the last line:

`#identify table`

x = table.Gender

#return Female value

x["Female",]

If we add `dim(x)`

to the code, we will see that it returns only a 3, that is 3 rows. There is not a second value because there is no additional column dimension. The solution therefore is to change the last line to `x["Female"]`

.

**Not equal to array extent / number of items to replace is not a multiple of replacement length**

Example:

The below code will produce a *length of 'dimnames' [1] not equal to array extent* error due to the last line:

#identify table

x = table.Living.arrangements

#new row names

newrows = c("Parents","Alone","Partner","Children","Partner + Children","Sharing","Other")

#rename rows

rownames(x) = newrows

If we add `length(rownames(x))`

and `length(newrows)`

to the code, we will see that one returns 8 and the other 7. We can only replace the row names with the exact same number of items. The solution is to either add "NET" to the end of the *newrows* object or remove it from the source table via right-click **> Hide**.

### Missing / duplicated rows or columns

Example:

The below code attempts to merge these two tables together but doesn't return the correct result:

#identify tables

x = table.Preferred.cola.2

y = table.Q4.Brand.Attitude.T2B

#combine tables

cbind(Preferred=x[rownames(y)],T2B=y)

If we add `head(x)`

and `head(y)`

, we can view the figures as they should align. However, this doesn't solve the immediate problem. We can also compare the row names by using the below to see what labels don't match:

`setdiff(rownames(x),rownames(y))`

setdiff(rownames(y),rownames(x))

What we can see here is that the *Pepsi* label has a trailing space in one table which causes the merged table not to align properly. The solution is to right-click **> Rename** this label in the preference table and remove the space.

## See Also

How to Work with R in Displayr

How to Extract Information from a Standard R Object

How to Work with R Data Types and Structures

## Comments

0 comments

Article is closed for comments.