The R language is a robust data analysis language, so when there is an error with the code, many times the error message returned is quite generic and technical. There are some error messages that do pop up fairly frequently though, and this article lists some of those and what types of issues they indicate.
Incorrect number of dimensions / subscript out of bounds
Missing / duplicated rows or columns
Can only convert tabular results to an R variable or question
Requirements
Please note this requires the Data Stories module or a Displayr license.
Familiarity with How to Use R in Displayr and How to Work with Data in R.
Incorrect number of dimensions / subscript out of bounds
This error indicates that what you're referencing inside the square brackets (used to subset data in R) is either:
1. Referenced incorrectly - such as in the example below. This is more of a syntax issue of how many commas you need inside [ ] to pull out the subset you need. You can use point-and-click or the following article on how to reference data in R: How to Work with Data in R.
2. Does not exist where it is referenced - such as listing a row name in the column part of the subsetting brackets [ ], referencing the 8th column of a table with less than 8 columns, misspelling a column or row name (watch out for trailing spaces!).
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 no second (column) 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 / replacement has X data has Y / non-conformable arrays
An important concept to understand with R coding is vectorization and recycling per this article: R's Vectorized Math and Custom Variable Creation, which says that the number of values/labels/whatever you provide to something must be the same as or a factor of the number of the original (though there are some exceptions). Examples of things that will break because of this include:
1. Exception for names(), colnames(), and rownames() - names do not recycle and number must match, so providing 1 label to relabel a table with 6 rows will return an error.
2. Providing 3 colors to an argument to color 4 lines (if you supplied 2 then those 2 colors would be repeated to make 4 colors total).
3. Assigning 3 numbers to a column in a data frame with 10 rows.
4. Performing a calculation on two tables with different number of rows/columns.
Example 1:
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.
Example 2:
Setting a column of data to different values with and without recycling.
Missing / duplicated rows or columns
There are various ways to merge data together using R, see How to Merge Tables Using R. However in all instances, if the data used to merge (usually it is the row/column names, but could also be a specific variable) isn't exactly matching, you will get unexpected results. You should always check your final result to ensure that it has the correct number of rows and columns that you expect.
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.
Can only convert tabular results to an R variable or question
When you create a new R variable (or multiple R variables) in your dataset, this is akin to inserting a column(s) in a table of raw data. So this new column will be expecting to receive values for each row (respondent) for each column. Thus, you need to ensure when creating new R variables that the final result of your R code matches the shape (#rows and #columns) of the variables you're creating. If it doesn't then the above error will be returned.
Keep in mind, Displayr will only "see" the output from the last line of your code so you should be sure that it references the entire variable and not a subset (if recoding using [] for subscripting). The easiest way to do this is to get in the habit of putting the variable name on the final line of your code.
Example:
When trying to create a dummy variable column of 1s in your data set, you will get an error if you simply put 1 in the code.
You need to modify the code so that the 1 value is repeated to account for all respondents (rows) in the data set. This is typically done using the rep function such as:
#repeat the 1 value based on the number (length) of Response Ids (RespId) in the data set
#RespId can be replaced with any variable in your data set as
#as they will all have the same length (number of rows)
rep(1, length(RespId))
Invalid token
There are a couple of causes of the invalid token error in Displayr:
1. More common - you are missing a closing parentheses ) or curly bracket } in your code. You can move your cursor next to the open ones ( and { to highlight the open and close pair. If you don't see a closing one or it closes in the wrong spot, add the correct parentheses or curly bracket to close it.
2. Less common - when copying and pasting code from different software, whitespaces may be encoded differently and not recognized by R. This usually is the cause when the error puts a ^ in the middle of a line of code to show where the token is located. You will need to delete and add back the space in Displayr's code editor to fix the issue.