# R Error: incorrect number of dimensions (2 Examples)

This tutorial explains how to avoid the error “incorrect number of dimensions” in the R programming language.

The table of content is structured as follows:

You’re here for the answer, so let’s get straight to the examples:

## Creation of Example Data

We’ll use the data below as basement for this R tutorial:

```x <- letters[1:5] # Create example vector x # Print example vector #  "a" "b" "c" "d" "e"```

Have a look at the previous output of the RStudio console. It shows that our example data is a character vector containing five vector elements.

## Example 1: Reproduce the Error – incorrect number of dimensions

In this example, I’ll show how to replicate the error message “incorrect number of dimensions” in R.

Let’s assume that we want to extract a subset of our vector using square brackets. Then, we might try to use a code like this:

```x[1:3, 1:3] # Try to access two dimensions of one-dimensional vector # Error in x[1:3, 1:3] : incorrect number of dimensions```

Unfortunately, the previous R code leads to the error message “incorrect number of dimensions”.

The reason for this is that we have tried to extract two dimensions from a one-dimensional data object (i.e. a vector). Remember that the comma (i.e. “,”) within square brackets tells R that we want to specify another dimension.

So how can we debug this error message and solve this problem?

## Example 2: Fix the Error – incorrect number of dimensions

In Example 2, I’ll explain how to deal with the error message “incorrect number of dimensions”.

To avoid this message, we simply have to specify the correct number of dimensions.

In this example, we are using a one-dimensional vector as basement. For that reason, we should only specify one dimension within the square brackets:

```x[1:3] # Properly access values of vector #  "a" "b" "c"```

The previous R code works fine!

## Video, Further Resources & Summary

If you need further info on the R programming code of this tutorial, you might watch the following video of my YouTube channel. In the video, I’m explaining the contents of this tutorial in a live session:

In addition, you may have a look at the related articles of this website. A selection of tutorials is listed below:

To summarize: In this article, I have illustrated how to handle the error message “incorrect number of dimensions” in the R programming language. Let me know in the comments below, if you have further questions.

Subscribe to the Statistics Globe Newsletter

• Bea Whitley
April 28, 2021 5:29 pm

Hi there, i am having trouble getting this warning message “Error in “ozone.txt”[, c(1, 2)] : incorrect number of dimensions” when trying to unstack my ozone.txt data.

and R has correctly read in the data as two columns, \$0zone, \$Garden.

For this problem, there are two gardens ‘A’ and ‘B’ with different values of ozone concentration, but I am trying to perform a t-test so would like the ozone concentrations organised under Garden type rather than two two just being 2 lists (sorry I know this is basic but I’m new to R!)

when I use the command
gardens1=unstack(“ozone.txt”[,c(1,2)])
i get the warning message.

I have used this before to unstack the data according to the 2nd column. I’m sure its simple but any guidance on the basics of unstacking is greatly appreciated.
Thanks

• Hi Bea,

Could you check the classes of your variables Ozone and Garden? You can do that by running the following line of code:

`sapply(your_data, class)`

What is the output of this line of code?

Regards

Joachim

• Richard
September 26, 2021 10:11 pm

HELLO,
I would like to ask how I can correct this error message of incorrect number of dimensions,

> lakes grid no_lake <- grid[length(st_within(grid, lakes)) == 0,]
Error in `[.default`(grid, length(st_within(grid, lakes)) == 0, ) :
incorrect number of dimensions