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

前端 未结 7 937
感动是毒
感动是毒 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: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.

提交回复
热议问题