Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering a numeric vector based on whether the number is an integer or not

I need a function (bespoke or from a package) that can filter a numeric vector based on whether the number is an integer or not.

So in the following vector

vec <- c(1.0, 5.0, 5.3, 10.0, 2.5)

I would want it to come out

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

integerFunction(vec)

[1]  TRUE  TRUE FALSE  TRUE FALSE

I know R has special classes for numeric and integers but that is not what I am talking about. I need to find integers within a numeric class vector (i.e. where the value after the decimal is greater than 0)

>Solution :

You may try using floor

integerFunction <- function(x) {
  x - floor(x) == 0
}
integerFunction(c(1.0, 5.0, 5.3, 10.0, 2.5))

[1]  TRUE  TRUE FALSE  TRUE FALSE
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading