下面是一個方法,比較使用head
和tail
排成對的行對。
# find Departures that match the Arrival in the next row
sames <- which(head(dat$Departure, -1) == tail(dat$Arrival, -1))
# keep pairs of rows that match, maintaining order with `sort`
dat[sort(unique(c(sames, (sames + 1)))),]
Departure Arrival
1 A B
2 B A
3 F G
4 G F
5 U V
6 V U
請注意,兩個變量必須是字符向量,而不是因子變量。如有必要,您可以使用as.character
將它們強制轉換爲字符。
數據
dat <-
structure(list(Departure = c("A", "B", "F", "G", "U", "V", "K",
"K"), Arrival = c("B", "A", "G", "F", "V", "U", "L", "L")), .Names = c("Departure",
"Arrival"), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8"))
來源
2017-08-08 14:48:44
lmo
在你有準備飛行中排7,8連續兩行的情況下,你後獲得第9行的實際回報呢,還是直接去到另一個航班? – Lamia
我在下面發佈了幾個選項,但我會說你會因爲發佈一些非常可以谷歌的東西而在這裏被撕掉。在20803之前其他人會這樣說,先嚐試搜索。 – ike
它轉到另一個航班。謝謝 – user159941