How to check if each element in a vector is integer or not in R?

前端 未结 7 739
感动是毒
感动是毒 2021-01-11 14:52

Say, I have a vector y, and I want to check if each element in y is integer or not, and if not, stop with an error message. I tried is.integer(y), but it does not work.

相关标签:
7条回答
  • 2021-01-11 15:01

    you could do:

       y <- c(3,3.1,1,2.3)
       (y - floor(y)) == 0
        [1]  TRUE FALSE  TRUE FALSE
    

    or

       (y - round(y)) == 0
    

    and if you want a single TRUE or FALSE for the whole thing, put it in all(), e.g.:

       all((y - round(y)) == 0)
        [1] FALSE
    
    0 讨论(0)
  • 2021-01-11 15:03

    checking the following helps with a crisp if condition which we can use on scripting.

    sff <- 5
    
    if(!(is.integer(sff) == is.character(sff))){ 
      sff
    } else {
      "hello"
    }
    

    gives

    hello
    

    sff <- 'a' gives 'a' as the result.

    0 讨论(0)
  • 2021-01-11 15:04

    The simplest (and fastest!) thing is probably this:

    stopifnot( all(y == floor(y)) )
    

    ...So trying it out:

    y <- c(3,4,9)
    stopifnot( all(y == floor(y)) ) # OK
    
    y <- c(3,4.01,9)
    stopifnot( all(y == floor(y)) ) # ERROR!
    

    If you want a better error message:

    y <- c(3, 9, NaN)
    if (!isTRUE(all(y == floor(y)))) stop("'y' must only contain integer values")
    
    0 讨论(0)
  • 2021-01-11 15:06

    Not sure which is faster Tim's way or this, but:

    > x <- 1:5
    > y <- c(x, 2.0)
    > z <- c(y, 4.5)
    > all.equal(x, as.integer(x))
    [1] TRUE
    > all.equal(y, as.integer(y))
    [1] TRUE
    > all.equal(z, as.integer(z))
    [1] "Mean relative difference: 0.1111111"
    > 
    

    or:

    all((z - as.integer(z))==0)
    
    0 讨论(0)
  • 2021-01-11 15:17

    If you have floating-point representation error, try:

    round( y, TOLERANCE.DIGITS ) %% 1 == 0
    

    In my application, I had seriously brutal floating-point representation error, such that:

    > dictionary$beta[3]
    [1] 89
    > floor(dictionary$beta[3])
    [1] 88
    > as.integer( dictionary$beta )[3]
    [1] 88
    > dictionary$beta[3] %% 1
    [1] 1
    

    the remainder divided by one was one. I found that I had to round before I took the integer. I think all of these tests would fail in the case where you wanted the above 89 to count as an integer. The "all.equal" function is meant to be the best way to handle floating-point representation error, but:

    all.equal( 88, 89 );
    

    as in my case, would have (and did) given a false negative for an integer value check.

    EDIT: In benchmarking, I found that:

    (x == as.integer(x)) 
    

    was universally the best performer.

    (x == floor(x))
    ((x - as.integer(x)) == 0)
    

    usually worked well, often just as fast.

    (x %% 1 <= tolerance)
    

    works, but not as quickly as the others

    !(is.character(all.equal(x, as.integer(x)))) 
    

    when the vector wasn't integers, had terrible performance (certainly because it goes to the trouble of estimating the difference).

    identical(x, as.integer(x)) 
    

    when the vector was all integer values, returned the incorrect result (assuming the question was meant to check for integer values, not integer types).

    0 讨论(0)
  • 2021-01-11 15:18

    Here's another way (using the same trick as Justin of comparing each number to that number coerced into the 'integer' type):

    R> v1 = c(1,2,3)
    R> v2 = c(1,2,3.5)
    R> sapply(v1, function(i) i == as.integer(i))
    [1] TRUE TRUE TRUE
    R> sapply(v2, function(i) i == as.integer(i))
    [1]  TRUE  TRUE FALSE
    

    To make your test:

    R> all(sapply(v2, function(i) i == as.integer(i)))
    [1] FALSE
    
    0 讨论(0)
提交回复
热议问题