如何使用order函数对负值进行排序?
我正在尝试使用 order
函数对表的一列进行排序,
a<-c("-2","-7","-4")
b<-c("9","-1","3")
z<-data.frame(a,b)
当我想按 a 列从最大到最小对 Z 进行排序时,它不起作用。该函数按负值的绝对值对负值进行排序。
z[order(z$a,decreasing=TRUE),]
I am trying to use order
function to order a column of the table,
a<-c("-2","-7","-4")
b<-c("9","-1","3")
z<-data.frame(a,b)
When I want to order Z by column a, from the largest to smallest, while it doesn't work. The function orders the negative value by its absolute value.
z[order(z$a,decreasing=TRUE),]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要将因子转换为数字,您必须按照常见问题解答的描述,首先转换为字符 tna,然后再转换为数字:(
说明:因子,当然除非它们是“有序因子”,否则不是有序的与“>”或“<”的比较将返回 NA。
您的努力是对 numeric 的内部强制,这与您的预期不同。
If you need to convert factors to numeric you must, as the FAQ describes, first convert to character tna then to numeric:
(Explanation: Factors, unless of course they are "ordered factors", are not ordered and comparisons with ">" or "<" will return NA.
What is being ordered in your effort was the internal coercion to numeric which was not as you expected. )
这是一个简单的向量:
Here is a simple vector: