Error in sample.int(length(x), size, replace, prob) : invalid first argument

25,931

Solution 1

It actually works as you intend. You can see it if you insert print(N)

> repeat{

    S<-sample(N ,1, replace = FALSE, prob = NULL)
    S
    if (S==1) {
        remove.value<-c(S,S+1)
    } else if (S==25) {
        remove.value<-c(S,S-1)
    }else {remove.value<-c(S-1,S,S+1)
    }
    remove.value

    N <- N [which(N %!in% remove.value)]          
    print(N)

    if (is.null(N)) break
}
 [1]  1  2  3  4  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 [1]  1  2  3  4  8  9 10 11 12 13 14 15 16 17 18 19 20 21 25
 [1]  1  2  3  4  8  9 13 14 15 16 17 18 19 20 21 25
 [1]  1  2  3  4  8  9 13 14 15 16 17 18 25
 [1]  1  2  3  4  8  9 16 17 18 25
[1]  1  2  3  4  8  9 25
[1]  1  2  8  9 25
[1]  8  9 25
[1] 8 9
integer(0)
Error in sample.int(length(x), size, replace, prob) : 
  invalid first argument

The error is caused by the final value of N, which is integer(0), and not NULL. If you use if (length(N)== 0) break instead of if (is.null(N)) break the code works without error message.

Solution 2

Removing the library Tidyverse solved this issue for me. The problem seems to be RStudio calling the sample.int() instead of simply sample(). Cheers!

Share:
25,931
Hope
Author by

Hope

Updated on August 05, 2021

Comments

  • Hope
    Hope over 2 years

    I am running the following lines of code and facing the above-mentioned error. Any ideas how to fix this ?

    install.packages("operator.tools")    
    X <- NULL
    S <- NULL
    remove.valuex <- NULL
    N <- seq.int(1,25)
    
    library(operator.tools)
    
    repeat {
    
    S<-sample(N ,1, replace = FALSE, prob = NULL)
    S
    if (S==1) {
    remove.value<-c(S,S+1)
    } else if (S==25) {
    remove.value<-c(S,S-1)
    }else {remove.value<-c(S-1,S,S+1)
    }
    remove.value
    
    N <- N [which(N %!in% remove.value)]          
    N
    
    if (is.null(N)) break
    }
    
  • Hope
    Hope about 9 years
    I am seeing an inconsistent behaviour from R studio. I am still seeing the error despite trying to use RStudent's suggestion: X <- 0 S <- NULL remove.valuex <- NULL N <- seq.int(1,25) repeat{ S<-sample(N ,1, replace = FALSE, prob = NULL) print(S) if (S==1) { remove.value<-c(S,S+1) } else if (S==25) { remove.value<-c(S,S-1) }else {remove.value<-c(S-1,S,S+1) } remove.value N <- N[which(!N %in% remove.value)] print(N) X <- X+1 print(X) if (length(N)== 1) break }
  • DatamineR
    DatamineR about 9 years
    I'm using RStudio too, and it works for me. Do you get the same error?
  • Hope
    Hope about 9 years
    Yep the same error but every now and then depending on the random sample I guess.
  • JMichael
    JMichael over 8 years
    I'm getting the same error. The biggest source of confusion for me is that the error message mentions sample.int, and I'm using sample. So it looks like either it's just a sloppy error message, or RStudio is trying to use the wrong function.
  • Jakob
    Jakob over 2 years
    same problem and that didn't help for me. Also, sample.int() is baseR