Error: `i` must have one dimension, not 2. in CreateDataPartition in R

Viewed 1946

I'm running a section of code which I have ran successfully multiple times before and I'm now getting an odd error which I don't understand:

### Data Splitting

trainIndex <- createDataPartition(dataset$isTerminated, p = .8, 
                                  list = FALSE, 
                                  times = 1)

head(trainIndex)

# Step 2: Create the training  and testing datasets

testData  <- dataset[-trainIndex, ]
trainingData <- dataset[trainIndex, ]

testData <- dataset[-trainIndex, ] works fine, but when I run trainingData <- dataset[trainIndex, ] gives me an error of Error:imust have one dimension, not 2.

I've checked the code against https://topepo.github.io/caret/data-splitting.html and it matches. Has anyone else come across it and found a solution?

4 Answers

Check the class of your data. If it's a tibble, convert it to a data.frame.

I got round it by using sample.split(). I'm not sure what caused the issue though.

sample          = sample.split(dataset$isTerminated, SplitRatio = 0.75)
trainingData    = subset(dataset, sample == TRUE)
testData        = subset(dataset, sample == FALSE)

This seems to work fine

The code works for a data.frame:

library(caret)
dataset = data.frame(isTerminated=sample(0:1,100,replace=TRUE),
value=runif(100))
trainIndex <- createDataPartition(dataset$isTerminated, p = .8, 
                                  list = FALSE, 
                                  times = 1)

testData  <- dataset[-trainIndex, ]
trainingData <- dataset[trainIndex, ]

trainIndex is a matrix and the subsetting with a matrix works for data.frames and matrices. What you have is a tibble:

library(tibble)
dataset = tibble(dataset)
dataset[trainIndex,]
Error: `i` must have one dimension, not 2.

I really have no clue why adding the -trainIndex would work.. You can see it also works if you convert it to a vector:

dataset[c(trainIndex),]
# A tibble: 80 x 2
   isTerminated  value
          <int>  <dbl>
 1            0 0.412 
 2            1 0.488 
 3            1 0.395 
 4            0 0.212 
 5            0 0.0229
 6            0 0.0395
 7            0 0.883 
 8            0 0.601 
 9            0 0.524 
10            0 0.998 
# … with 70 more rows

Hopefully somehow more well versed in tibble can chip in, but yes, bottom line is be aware whether it is a data.frame or tibble..

This looks to me like a regression, as I also have the same error with previously working code.

# df is a tibble
t <- df[df[, 'condition'] == 'foo','t'][[1]][[1]]

Error: `i` must have one dimension, not 2.

As others have said, the workaround is to convert the tibble to a data frame:

df <- as.data.frame(df)
t <- df[df[, 'condition'] == 'foo','t'][[1]][[1]]

I'm not sure if this sort of dereferencing is supposed to work with tibbles, or whether this is a bug in recent tibble code. Either way, the error message isn't very helpful. Something like "You are trying to do something with a tibble that is only possible with a data frame." would be better.

Related