英文:
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:
> 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
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论