在R中如何查找列中非分数值的出现次数?

huangapple go评论71阅读模式
英文:

How to find occurrences of non-fraction values in a column in R?

问题

以下是翻译好的部分:

这应该很简单,但我找不到任何方法来实现这一点。我只想找到y列中包含非分数值的行。我的数据如下:

> df <- data.frame(id = c("a", "a", "a", "a", "a", "b", "b", "b", "b"),
                 y = c(1.000, 2.000, 3.000, 4.000, 5.345, 1.000, 2.000, 3.000, 4.670))
> df
id      y
 a  1.000
 a  2.000
 a  3.000
 a  4.000
 a  5.345
 b  1.000
 b  2.000
 b  3.000
 b  4.670

所需输出:

id      y
 a  5.345
 b  4.670

最好使用tidyverse方法。谢谢!

英文:

This should be easy, but I couldn't find any way to do this. All I want to do is find the rows with non-fraction values in y. I have data like so:

&gt; df &lt;- data.frame(id = c(&quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;, &quot;b&quot;),
                 y = c(1.000, 2.000, 3.000, 4.000, 5.345, 1.000, 2.000, 3.000, 4.670))
&gt; df
id      y
 a  1.000
 a  2.000
 a  3.000
 a  4.000
 a  5.345
 b  1.000
 b  2.000
 b  3.000
 b  4.670

Output required:

id      y
 a  5.345
 b  4.670

A tidyverse method would be preferred. Thanks!

答案1

得分: 3

筛选与取模:

filter(df, !near(y %% 1, 0))
英文:

Filter with modulo:

filter(df, !near(y %% 1, 0))

答案2

得分: 1

可能 `as.character` 在这里起到了一个技巧

> subset(df, grepl(".", as.character(y), fixed = TRUE))
id y
5 a 5.345
9 b 4.670


<details>
<summary>英文:</summary>

Probably `as.character` could play a trick here

> subset(df, grepl(".", as.character(y), fixed = TRUE))
id y
5 a 5.345
9 b 4.670


</details>



huangapple
  • 本文由 发表于 2023年7月18日 16:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定