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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With