Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove doubles with no decimal places

I have a vector:

x <- c(0.0, 0.5, 1.000, 1.5, 1.6, 1.7, 1.75, 2.0, 2.4, 2.5, 3.0, 74.0)

How can I extract only the values of x which contain nonzero values after the decimal place? For example, the resultant vector would look like this:

c(0.5, 1.5, 1.6, 1.7, 1.75, 2.4, 2.5)

Which has removed 0.0, 1.000, 2.0, 3.0, and 74.0.

like image 618
John L. Godlee Avatar asked Mar 26 '18 11:03

John L. Godlee


1 Answers

alternatively

x[x %% 1 != 0]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50

or

x[trunc(x) != x]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50

or

x[as.integer(x) != x]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50

or ( now I stop!)

x[grepl("\\.[^0]+$",x)]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50

:D

like image 60
Andre Elrico Avatar answered Sep 24 '22 17:09

Andre Elrico