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.
相关问题
- Do the Java Integer and Double objects have unnece
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
The simplest (and fastest!) thing is probably this:
...So trying it out:
If you want a better error message:
I went in a completely different direction then Tim (I like his better though my approach works on a mixed vector that's a character vector with integers etc.):
EDIT: altered the function as it only worked on character vectors.
This works on vectors of the class character as well in case you have a character vector with various number intermixed but that have been coerced to character.
checking the following helps with a crisp if condition which we can use on scripting.
gives
sff <- 'a'
gives'a'
as the result.If you have floating-point representation error, try:
In my application, I had seriously brutal floating-point representation error, such that:
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:
as in my case, would have (and did) given a false negative for an integer value check.
EDIT: In benchmarking, I found that:
was universally the best performer.
usually worked well, often just as fast.
works, but not as quickly as the others
when the vector wasn't integers, had terrible performance (certainly because it goes to the trouble of estimating the difference).
when the vector was all integer values, returned the incorrect result (assuming the question was meant to check for integer values, not integer types).
Here's another way (using the same trick as Justin of comparing each number to that number coerced into the 'integer' type):
To make your test:
you could do:
or
and if you want a single
TRUE
orFALSE
for the whole thing, put it inall()
, e.g.: