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
# [1] "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
# [1] "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:

 

The YouTube video will be added soon.

 

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

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


17 Comments. Leave new

  • 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.

    data is read in as gardens=read.table(“ozone.txt”, header=TRUE)
    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

    Reply
    • 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

      Reply
  • 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

    Reply
    • Hey Richard,

      Could you illustrate the data structure of the data you are using? Is grid a data.frame?

      Regards

      Joachim

      Reply
      • Hi Joachim,

        sorry for delay,
        the type data of grid, is a list,

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

        Look forward of your support, I

        Reply
        • Hey Richard,

          No problem! What’s the output when you run the following line of code?

          lengths(st_within(grid, lakes)) == 0

          It should be a vector of logical values (i.e. TRUE and FALSE).

          Regards

          Joachim

          Reply
        • Oh, and another thing I have just noticed: Does it work when you remove the comma at the end of your code?

          no_lake <- grid[lengths(st_within(grid, lakes)) == 0]

          Regards

          Joachim

          Reply
  • Hello Joachim,

    Both codes you give me, are working correctly (output are the same). the error message is solved.

    Thank you so much for your support.

    Richard/Live in Rwanda.

    Reply
  • Hello Joachim,
    I am running the following code:

    >library(mgarchBEKK)
    >library(vars)
    >data1 = ts(log(data$X))
    >data2 = ts(log(data$Y))
    >data3 = cbind(X, Y)
    >var = VAR(data3,type=c(“const”),lag.max=6,ic=”SC”)
    >bekk = BEKK(var,order = c(1, 1), params = NULL, fixed = NULL, method = “BFGS”, verbose = F)

    However, it gives me this error:
    Error in eps[, 1] : incorrect number of dimensions
    ————
    I also want to mention that for the BEKK function, the first argument should be “Data frame holding time series”. However, I do not know how I convert the VAR model to a data frame.

    Reply
  • Hello Joachim,
    I am trying to run a Dun`s correction test for the non-parametric Scheirer-ray-Hare`s test. However, I
    I have an error message about incorrect dimensions. Below is the code and what the error message says.
    DT = dunnTest( aw ~ Chunk.type , data=data, method=”bh”)
    Error in Psort[1, i] : incorrect number of dimensions
    In addition: Warning message:
    Chunk.type was coerced to a factor.
    Please how can I correct this?

    Kind regards,
    Marisa

    Reply
    • Hey Marisa,

      Unfortunately, I have never applied the dunnTest function myself. Based on the error message it seems like the data in Psort is not formatted properly.

      However, I have recently created a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well, and maybe you get better help there: https://www.facebook.com/groups/statisticsglobe

      Regards,
      Joachim

      Reply
  • Hi Joachim,

    I have some problem with time series plotting with regression.

    data_all1 = ts(HNE.df, start = c(2021,03),frequency = 365)
    linear.fit = tslm(data_all1~trend,data = data_all1)
    summary(linear.fit)

    however, it says Error in resid[, i] : incorrect number of dimensions

    Reply
  • Hi Joachim,
    It shows that this:
    > head(data_all1)
    Time Series:
    Start = c(2021, 3)
    End = c(2021, 8)
    Frequency = 365
    Time Wo_IS_temperature_9am Wo_IS_temperature_3pm Wo_IS_humidity_9am Wo_IS_humidity_3pm
    2021.005 152 22.8 25.3 79 80
    2021.008 174 20.2 20.0 72 54
    2021.011 196 17.3 20.7 66 55
    2021.014 202 20.9 24.0 59 57
    2021.016 204 21.8 20.0 58 55
    2021.019 206 18.6 21.1 60 52
    Wo_IS_SO2 Wo_IS_NO Wo_IS_NO2 Wo_IS_OZONE Wo_IS_PM10 Wo_IS_PM2.5 Wo_IS_CO Wo_IS_cases
    2021.005 0.1 0.1 0.3 2.2 26.70000 5.536504 0.2 0
    2021.008 0.2 0.2 0.4 1.6 14.90127 5.536504 0.2 0
    2021.011 0.0 0.1 0.2 1.7 14.90127 5.536504 0.1 0
    2021.014 0.1 0.4 0.7 2.0 21.50000 5.536504 0.1 0
    2021.016 0.2 0.2 0.4 1.6 25.70000 5.536504 0.2 0
    2021.019 0.1 0.1 0.1 1.8 21.90000 6.700000 0.2 0

    But Thank you ! I have just fixed it by changing the data_all1 to one of the variables. Now I have some graphs now. Could you tell me how to fit GAM on the base of time series data?

    Sincerely,
    Mark

    Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top