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

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

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

问题

以下是翻译好的部分:

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

  1. > df <- data.frame(id = c("a", "a", "a", "a", "a", "b", "b", "b", "b"),
  2. y = c(1.000, 2.000, 3.000, 4.000, 5.345, 1.000, 2.000, 3.000, 4.670))
  3. > df
  4. id y
  5. a 1.000
  6. a 2.000
  7. a 3.000
  8. a 4.000
  9. a 5.345
  10. b 1.000
  11. b 2.000
  12. b 3.000
  13. b 4.670

所需输出:

  1. id y
  2. a 5.345
  3. 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:

  1. &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;),
  2. y = c(1.000, 2.000, 3.000, 4.000, 5.345, 1.000, 2.000, 3.000, 4.670))
  3. &gt; df
  4. id y
  5. a 1.000
  6. a 2.000
  7. a 3.000
  8. a 4.000
  9. a 5.345
  10. b 1.000
  11. b 2.000
  12. b 3.000
  13. b 4.670

Output required:

  1. id y
  2. a 5.345
  3. b 4.670

A tidyverse method would be preferred. Thanks!

答案1

得分: 3

筛选与取模:

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

Filter with modulo:

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

答案2

得分: 1

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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

  1. </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:

确定