总结():捕获应该是唯一的非唯一值
使用总结()
和group_by
,我想捕获应该是唯一的非唯一值。
示例:
dd <- data.frame(person=rep(1:3, each=2),
place=rep(c("earth", "moon"), times=3),
height=rep(c(60, 70, 80), each=2),
weight=c(10, 60, 12, 72, 15, 90))
dd
person place height weight
1 1 earth 60 10
2 1 moon 60 60
3 2 earth 70 12
4 2 moon 70 72
5 3 earth 80 15
6 3 moon 80 90
这可以正常工作:
byPerson <- summarise(.data=group_by(.data=dd, person),
height=unique(height))
byPerson
person height
1 1 60
2 2 70
3 3 80
但是:
byPerson2 <- summarise(.data=group_by(.data=dd, person),
height=unique(height), weight=unique(weight))
dplyr
的早期版本曾经失败,因为权重
在Person
中不是唯一的。当前版本给出了此结果,
person height weight
1 1 60 10
2 1 60 60
3 2 70 12
4 2 70 72
5 3 80 15
6 3 80 90
而不是通知用户错误。
是否有一些重新获得早期行为的方法?最好通过设置标志或其他东西,因为我可以检查代码中的非唯一值,但这更痛苦。
Using summarise()
and group_by
, I want to catch non-unique values that should be unique.
Example:
dd <- data.frame(person=rep(1:3, each=2),
place=rep(c("earth", "moon"), times=3),
height=rep(c(60, 70, 80), each=2),
weight=c(10, 60, 12, 72, 15, 90))
dd
person place height weight
1 1 earth 60 10
2 1 moon 60 60
3 2 earth 70 12
4 2 moon 70 72
5 3 earth 80 15
6 3 moon 80 90
This works just fine:
byPerson <- summarise(.data=group_by(.data=dd, person),
height=unique(height))
byPerson
person height
1 1 60
2 2 70
3 3 80
But:
byPerson2 <- summarise(.data=group_by(.data=dd, person),
height=unique(height), weight=unique(weight))
Earlier versions of dplyr
used to fail, because weight
is not unique within person
. The current version gives this result,
person height weight
1 1 60 10
2 1 60 60
3 2 70 12
4 2 70 72
5 3 80 15
6 3 80 90
instead of notifying the user of the error.
Is there some way of regaining the earlier behavior? Preferably by setting a flag or something, since I could check for non-unique values in code but that's more of a pain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论