我正在創建一個R sweave文件,該文件將編譯一份軟件中測試數據的pdf報告。這些數據主要是從看起來是這樣的一個SQL Server表拉:查找第二高值而不是最大值「-1」
| FileName | Version | Category | Value | Date | TestNum |
|:--------:|:-------:|:--------:|:-----:|:-------------------:|:-------:|
| File1 | 1.0.12 | Run Time | 74 | 2016-10-01 12:00:00 | 1 |
| File1 | 1.0.12 | Totals | 468 | 2016-10-01 12:00:00 | 1 |
| File1 | 1.0.12 | DB Size | 589 | 2016-10-01 12:00:00 | 1 |
| File2 | 1.0.12 | Run Time | 81 | 2016-10-01 12:00:00 | 1 |
| File2 | 1.0.12 | Totals | 351 | 2016-10-01 12:00:00 | 1 |
| File2 | 1.0.12 | DB Size | 625 | 2016-10-01 12:00:00 | 1 |
| File1 | 1.0.15 | Run Time | 74 | 2016-10-01 12:00:00 | 2 |
| File1 | 1.0.15 | Totals | 468 | 2016-10-01 12:00:00 | 2 |
| File1 | 1.0.15 | DB Size | 589 | 2016-10-01 12:00:00 | 2 |
| File2 | 1.0.15 | Run Time | 81 | 2016-10-01 12:00:00 | 2 |
| File2 | 1.0.15 | Totals | 351 | 2016-10-01 12:00:00 | 2 |
| File2 | 1.0.15 | DB Size | 625 | 2016-10-01 12:00:00 | 2 |
| File1 | 1.0.17 | Run Time | 74 | 2016-10-01 12:00:00 | 3 |
| File1 | 1.0.17 | Totals | 468 | 2016-10-01 12:00:00 | 3 |
| File1 | 1.0.17 | DB Size | 589 | 2016-10-01 12:00:00 | 3 |
| File2 | 1.0.17 | Run Time | 81 | 2016-10-01 12:00:00 | 3 |
| File2 | 1.0.17 | Totals | 351 | 2016-10-01 12:00:00 | 3 |
| File2 | 1.0.17 | DB Size | 625 | 2016-10-01 12:00:00 | 3 |
| File1 | 1.0.21 | Run Time | 74 | 2016-10-01 12:00:00 | 4 |
| File1 | 1.0.21 | Totals | 468 | 2016-10-01 12:00:00 | 4 |
| File1 | 1.0.21 | DB Size | 589 | 2016-10-01 12:00:00 | 4 |
| File2 | 1.0.21 | Run Time | 81 | 2016-10-01 12:00:00 | 4 |
| File2 | 1.0.21 | Totals | 351 | 2016-10-01 12:00:00 | 4 |
| File2 | 1.0.21 | DB Size | 625 | 2016-10-01 12:00:00 | 4 |
我用TestNum列,使其更容易以遞增數版本,因爲它們是字符串。所以在我的R腳本中,我有一個本應該找到最新版本的部分,以及之前的版本。
vLatest <- unique(df[df[,"TestNum"] == max(df$TestNum), "Version"])
vPrevious <- unique(df[df[,"TestNum"] == max(df$TestNum)-1, "Version"])
但是,有時候某個版本的軟件對於每個測試來說都是非常麻煩的並且崩潰。這是不是在圖表看,所以我剛剛加入,我用它來篩選出來的SQL數據庫中的線是非常有用的,然後將R數據幀最終看起來像這樣:
| FileName | Version | Category | Value | Date | TestNum |
|:--------:|:-------:|:--------:|:-----:|:-------------------:|:-------:|
| File1 | 1.0.12 | Run Time | 74 | 2016-10-01 12:00:00 | 1 |
| File1 | 1.0.12 | Totals | 468 | 2016-10-01 12:00:00 | 1 |
| File1 | 1.0.12 | DB Size | 589 | 2016-10-01 12:00:00 | 1 |
| File2 | 1.0.12 | Run Time | 81 | 2016-10-01 12:00:00 | 1 |
| File2 | 1.0.12 | Totals | 351 | 2016-10-01 12:00:00 | 1 |
| File2 | 1.0.12 | DB Size | 625 | 2016-10-01 12:00:00 | 1 |
| File1 | 1.0.15 | Run Time | 74 | 2016-10-01 12:00:00 | 2 |
| File1 | 1.0.15 | Totals | 468 | 2016-10-01 12:00:00 | 2 |
| File1 | 1.0.15 | DB Size | 589 | 2016-10-01 12:00:00 | 2 |
| File2 | 1.0.15 | Run Time | 81 | 2016-10-01 12:00:00 | 2 |
| File2 | 1.0.15 | Totals | 351 | 2016-10-01 12:00:00 | 2 |
| File2 | 1.0.15 | DB Size | 625 | 2016-10-01 12:00:00 | 2 |
| File1 | 1.0.21 | Run Time | 74 | 2016-10-01 12:00:00 | 4 |
| File1 | 1.0.21 | Totals | 468 | 2016-10-01 12:00:00 | 4 |
| File1 | 1.0.21 | DB Size | 589 | 2016-10-01 12:00:00 | 4 |
| File2 | 1.0.21 | Run Time | 81 | 2016-10-01 12:00:00 | 4 |
| File2 | 1.0.21 | Totals | 351 | 2016-10-01 12:00:00 | 4 |
| File2 | 1.0.21 | DB Size | 625 | 2016-10-01 12:00:00 | 4 |
但隨後vPrevious
是仍在尋找TestNum == 3
,因此腳本中斷。有沒有一種方法可以查找第二高的值呢?
編輯:通過建議,這裏是什麼查詢看起來像我用來創建數據框。
df <- sqlQuery(db, "select FileName, Version, Category, Value, Date, TestNum
from Table where Comments != 'Do Not Include in R Chart'",
stringsAsFactors = F)
vPrevious < - 獨特(DF [DF [ 「TestNum」] ==排序(獨特(DF $ TestNum),T)[2], 「版本」]) – dww
@dww那是我溶液正在尋找感謝! – David
@大衛我已根據您的查詢更新了Sql。雖然您可以選擇在Sql和R中實現相同的結果,但您可以花一點時間比較結果和性能?可能會提供一些有趣的見解...乾杯 –