我有一些GIS数据,其中包含始发地和目的地(OD)以及有关OD一天中的时间的信息。我打算对此进行映射,并根据每天的时间对OD进行着色。
一件事是,某些OD在白天和黑夜中都有可能以不同的顺序出现。我想以不同的方式标记这些标记,例如“日/夜”
是否有简单的方法可以做到这一点?我的MWE只是一个OD,但我需要在其他几个中进行识别。无论顺序如何,我都可以找到重复项,但是我不知道如何找出是否存在两个时间案例,以及如何用“日/夜”替换它们。
library(data.table)
Origin<-c("London", "Paris")
Destination<-rev(Origin)
dt<-data.table(Origin=Origin, Destination=Destination, Time=c("Day", "Night"))
#duplicates regardless of order
dat.sort = t(apply(dt[,.(Origin,Destination)], 1, sort))
dt[duplicated(dat.sort) | duplicated(dat.sort, fromLast=TRUE),]
´´´
您可以使用dplyr软件包,如下所述:
根据您的需要随意更改条件。
library(data.table)
library(dplyr)
# Creating data
dt <-
data.table(
Origin = c("London", "Paris", "Italy", "Spain", "Portugal", "Poland"),
Destination = c("Paris", "London", "Norway", "Portugal", "Spain", "Spain"),
Time = c("Day", "Night", "Day", NA_character_, NA_character_, NA_character_)
)
dt
# Origin Destination Time
# London Paris Day
# Paris London Night
# Italy Norway Day
# Spain Portugal <NA>
# Portugal Spain <NA>
# Poland Spain <NA>
dt %>%
# pmin and pmax are used to sort the 2 columns
# in order to group by them regardless to their order
group_by(Origin2 = pmin(Origin, Destination),
Destination2 = pmax(Origin, Destination)) %>%
mutate(count = n(), # To check if Origin/destination are repeated or not
row = row_number(), # Place holder to know if it was first to repeat or second
# If not repeated then make Time = Day
# If repeated and first occurance then Time = Day
# If repeated and second occurance then Time = Night
Time = case_when(count == 1 ~ "Day",
count == 2 & row == 1 ~ "Day",
count == 2 & row == 2 ~ "Night")) %>%
ungroup() %>%
select(Origin, Destination, Time)
# Origin Destination Time
# <chr> <chr> <chr>
# 1 London Paris Day
# 2 Paris London Night
# 3 Italy Norway Day
# 4 Spain Portugal Day
# 5 Portugal Spain Night
# 6 Poland Spain Day