2

I am getting the following error:

Error in sample.split: 'SplitRatio' parameter has to be i [0, 1] range or [1, length(Y)] range

when I try to run the following code:

set.seed(1000)
library(caTools)
split = sample.split(letters$isB, SplitRatio = 0.5)
pp_
  • 3,435
  • 4
  • 19
  • 27
Superad
  • 61
  • 1
  • 1
  • 4

7 Answers7

10

There is nothing wrong with the syntax. You probably spelled your outcome variable (letters$isB) incorrectly. Since letters$isB does not exist (or not loaded), you get that error.

Mahesh Mitikiri
  • 126
  • 2
  • 5
1

I received an error similar to what was listed above. I realized that I had forgotten to change my variable in the code listed below

split = sample.split(dataset$Profit, 

from profit to units sold (variable in my actual data set) vs. profit which was code from another project. Hope this helps - I listed the rest of my code and my errors below.

> library(caTools)
> set.seed(123)
> split = sample.split(dataset$Profit, SplitRatio = .8)
Error in sample.split(dataset$Profit, SplitRatio = 0.8) : 
  Error in sample.split: 'SplitRatio' parameter has to be i [0, 1] range or [1, length(Y)] range
> training_set = subset(dataset, split == TRUE)
Error in split == TRUE : 
  comparison (1) is possible only for atomic and list types
> test_set = subset(dataset, split == FALSE)
Error in split == FALSE : 
  comparison (1) is possible only for atomic and list types
Anton Dementiev
  • 5,451
  • 4
  • 20
  • 32
0

Maybe letters$isB length is 0?

Alessandro Cuttin
  • 3,822
  • 1
  • 30
  • 36
Juanje
  • 1
0

The previous answer (https://stackoverflow.com/a/35706404/6188234)

"maybe letters$isB length is 0?"

makes sense with more context. In my experience with another MOOC I received this error, and came to SO looking for an answer.

After referring to Sample.split in R - SplitRatio parameter

I noted that the error is generated if the variable you are trying to split doesn't exist - because of a typo. So the error message misleads the coder to look at the SplitRatio Constant, instead of the variable you are splitting.

split = sample.split(letters$THISDOESNOTEXIST, SplitRatio = 0.5)

In my case this typo was the camelCase of the variable name, so it was difficult to seethe syntax error. Fixing that type cleared this error.

I hope this works for you.

Community
  • 1
  • 1
0

I have exactly the same problem, and I am sure nothing is wrong with the syntax nor with the variables. More interestingly, the code works if I run the related chunk manually on Rmarkdown, but when I run the whole markdown from top to bottom, it returns error.

Shahin
  • 428
  • 4
  • 9
0

set.seed(1000) library(caTools) split = sample.split(letters$isB, SplitRatio = 0.5)

isB should be the label of the Dependent variable, look up in your dataset that name.

Here you can find why this error is raised.

Anant
  • 396
  • 4
  • 11
0

I had this same problem. Make sure you've dropped or omitted all your NA values.

Antonio
  • 417
  • 2
  • 8