简体   繁体   English

data.table通过另一个条件中的一个条件选择行

[英]data.table select rows by a condition on the other condition

I have a given datatable 我有给定的数据表

A  B
1  2
3  4
3  2
2  1

I would like to select the row with the maximum A. If there is more than one row with the maximum A, I will select the row with the maximum B from the selected rows. 我想选择最大A的行。如果存在多于最大A的行,我将从选定的行中选择最大B的行。 Here should get 这里应该得到

A  B
3  4

How should I achieve it using data.table ? 我应该如何使用data.table实现它?

Data: 数据:

x <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "
A  B
1  2
3  4
3  2
2  1")

Working code: 工作代码:

library(data.table)
x[ head(order(A, B, decreasing = TRUE), n = 1), ]
#    A B
# 1: 3 4

An alternative: 替代:

x[ order(A, B, decreasing = TRUE)[1], ]

is valid, but produces a row of NA when nrow(x)==0 , where I would prefer/expect 0 rows. 是有效的,但是当nrow(x)==0时会产生NA行,我希望/期望0行。 For example: 例如:

x[0,][ order(A, B, decreasing = TRUE)[1], ]
#     A  B
# 1: NA NA
x[0,][ head(order(A, B, decreasing = TRUE), n = 1), ]
# Empty data.table (0 rows) of 2 cols: A,B

Another alternative, my first suggestion: 另一个选择,我的第一个建议:

x[ order(A, B, decreasing = TRUE), ][1,]

It is perfectly legitimate but as @thelatemail suggested (thanks!), it is far less efficient in that it produces the whole (restructured) frame before giving you the first row. 它是完全合法的,但是正如@thelatemail所建议的(谢谢!),它的效率要低得多,因为它会在给您第一行之前生成整个(重组的)框架。

Yet another alternative (from the crowd, thanks again thelatemail): 另一个选择(从人群中,再次感谢thelatemail):

setorder(x, -A, -B)[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM