我想通過2個data.tables的組進行子集/匹配數據,並且無法弄清楚這是如何在R中的。 我有以下數據.table有一個City_ID和一個時間戳(列名=時間)。R data.table加入/子集/按組和條件匹配
Library(data.table)
timetable <- data.table(City_ID=c("12","9"),
Time=c("12-29-2013-22:05:03","12-29-2013-11:59:00"))
我有第二個data.table與幾個觀察城市和時間戳(加上額外的數據)。該表是這樣的:
DT = data.table(City_ID =c("12","12","12","9","9","9"),
Time= c("12-29-2013-13:05:13","12-29-2013-22:05:03",
"12-28-2013-13:05:13","12-29-2013-11:59:00",
"01-30-2013-10:05:03","12-28-2013-13:05:13"),
Other=1:6)
現在我需要找到每個城市DT有一個時間> =時間在其他data.table「時間表」(這基本上是對照表)的意見。只應保留這些記錄(包括未用於計算的列;在「其他」示例列中)。我想要的結果看起來是這樣的:
desiredresult = data.table(City_ID=c("12","9"),
Time= c("12-29-2013-22:05:03","12-29-2013-11:59:00"),
Other=c("2","4"))
我曾嘗試以下:
setkey(DT, City_ID, Time)
setkey(timetable, City_ID)
failedresult = DT[,Time >= timetable[Time], by=City_ID]
failedresult2 = DT[,Time >= timetable, by=City_ID]
BTW:我知道這可能是更好的附加分離的日期和時間,但是這可能讓這個例子甚至更復雜(當我測試通過data.table在時間戳中找到最小值時,它似乎工作)。
對不起,我發現我在老版本的「desiredresult」 ..現在糾正了一個微小的錯誤。 – user3340145