Delete doubles without decimals

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 x values โ€‹โ€‹that contain non-zero values โ€‹โ€‹after the decimal point? For example, the resulting vector will look like this:

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

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

+5
source share
2 answers

We can build a logical index with round

 x[round(x) != x] #[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50 
+8
source

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

+8
source

Source: https://habr.com/ru/post/1276089/


All Articles