删除没有小数位的双打

Joh*_*lee 5 regex grep r data-manipulation

我有一个矢量:

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)
Run Code Online (Sandbox Code Playgroud)

如何仅提取x小数位后包含非零值的值?例如,结果向量将如下所示:

c(0.5, 1.5, 1.6, 1.7, 1.75, 2.4, 2.5)
Run Code Online (Sandbox Code Playgroud)

其中已移除0.0,1.000,2.0,3.0,和74.0.

And*_*ico 9

或者

x[x %% 1 != 0]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50
Run Code Online (Sandbox Code Playgroud)

要么

x[trunc(x) != x]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50
Run Code Online (Sandbox Code Playgroud)

要么

x[as.integer(x) != x]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50
Run Code Online (Sandbox Code Playgroud)

或者(现在我停下来!)

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

:d


akr*_*run 8

我们可以构造一个逻辑索引 round

x[round(x) != x]
#[1] 0.50 1.50 1.60 1.70 1.75 2.40 2.50
Run Code Online (Sandbox Code Playgroud)