我有一个小的数据框,其中包含学习系统两侧的两个多边形的点,并在下图显示(东为蓝色,西为红色)。这是数据框。
structure(list(Shore = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("East", "West"), class = "factor"), Lat = c(72.45604629,
72.44350398, 72.41516226, 72.36734761, 72.33600228, 72.31760236,
72.29050994, 72.26066187, 72.2680832, 72.47144353, 72.45604629,
72.45604629, 72.44816356, 72.37238665, 72.24127983, 72.26066187,
72.29050994, 72.31760236, 72.33600228, 72.36734761, 72.41516226,
72.44350398, 72.45604629, 72.45604629), Long = c(-80.89019727,
-80.91753044, -80.9496172, -81.0363703, -81.1243118, -81.15639857,
-81.16947095, -81.27702103, -81.33465836, -80.90326966, -80.89019727,
-80.89019727, -80.84860332, -80.94367521, -81.28474562, -81.27702103,
-81.16947095, -81.15639857, -81.1243118, -81.0363703, -80.9496172,
-80.91753044, -80.89019727, -80.89019727)), class = "data.frame", row.names = c(NA,
-24L))
我还有一个动物运动的数据框,可以在地图上显示的每个点处检测到。我想知道的是,系统中何时何地动物从红色越过蓝色,反之亦然。以下是我的数据的一小部分。所有数据应落在两个多边形之一(红色或蓝色)内。 ID列是个体动物的ID,而Station列则指示哪个接收者(A最北,1最西)。我想最后我会在寻找一个新的数据框,其中的列显示这些事件发生的日期/时间,发生移动的位置以及移动的人。
structure(list(DateTime = structure(c(1503006715, 1503006880,
1503007037, 1503007108, 1503007185, 1503007255, 1503007331, 1503007399,
1503007554, 1503007633, 1503007709, 1503007775, 1503007845, 1503007987,
1503008057, 1503008132, 1503008199, 1503008269, 1503008392, 1503008412,
1503008544, 1503008620, 1503009148, 1503009217, 1503009291, 1503009356,
1503009376, 1503009421, 1503009488, 1503009508, 1503009558, 1503009578,
1503009634, 1503009702, 1503009722, 1503009774, 1503009854, 1503009875,
1503009932, 1503010003, 1503010023, 1503010081, 1503010101, 1503010153,
1503010234, 1503010254, 1503010312, 1503010332, 1503010383, 1503010463,
1503010483, 1503010538, 1503015897, 1503015963, 1503016024, 1503016873,
1503017027, 1503017229, 1503022094, 1503022380, 1503022393, 1503022476,
1503022559, 1503022641, 1503022721, 1503022785, 1503022798, 1503022855,
1503022868, 1503022931, 1503022944, 1503023000, 1503023013, 1503023073,
1503023086, 1503023155, 1503023168, 1503023235, 1503023313, 1503023383,
1503023397, 1503023461, 1503023474, 1503023533, 1503023612, 1503023625,
1503023686, 1503023816, 1503024252, 1502754012, 1502754224, 1502754364,
1502754444, 1502754588, 1502754661, 1502754742, 1502754822, 1502758872,
1502758944, 1502758971), class = c("POSIXct", "POSIXt"), tzone = "America/New_York"),
ID = c(1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L, 1657L,
1658L, 1658L, 1658L, 1658L, 1658L, 1658L, 1658L, 1658L, 1658L,
1658L, 1658L), Station = c("TRD1", "TRD1", "TRD1", "TRD1",
"TRD1", "TRD1", "TRD1", "TRD1", "TRD1", "TRD1", "TRD1", "TRD1",
"TRD1", "TRD1", "TRD1", "TRD1", "TRD1", "TRD1", "TRD2", "TRD1",
"TRD2", "TRD2", "TRD2", "TRD2", "TRD2", "TRD2", "TRD1", "TRD2",
"TRD2", "TRD1", "TRD2", "TRD1", "TRD2", "TRD2", "TRD1", "TRD2",
"TRD2", "TRD1", "TRD2", "TRD2", "TRD1", "TRD2", "TRD1", "TRD2",
"TRD2", "TRD1", "TRD2", "TRD1", "TRD2", "TRD2", "TRD1", "TRD2",
"TRE5", "TRE5", "TRE4", "TRE4", "TRE4", "TRE4", "TRF3", "TRF4",
"TRF3", "TRF3", "TRF3", "TRF3", "TRF3", "TRF4", "TRF3", "TRF4",
"TRF3", "TRF4", "TRF3", "TRF4", "TRF3", "TRF4", "TRF3", "TRF4",
"TRF3", "TRF4", "TRF4", "TRF4", "TRF3", "TRF4", "TRF3", "TRF4",
"TRF4", "TRF3", "TRF4", "TRF4", "TRF4", "TRD1", "TRD1", "TRD1",
"TRD1", "TRD1", "TRD1", "TRD1", "TRD1", "TRE2", "TRE2", "TRE1"
), Latitude = c(72.35019, 72.35019, 72.35019, 72.35019, 72.35019,
72.35019, 72.35019, 72.35019, 72.35019, 72.35019, 72.35019,
72.35019, 72.35019, 72.35019, 72.35019, 72.35019, 72.35019,
72.35019, 72.34705, 72.35019, 72.34705, 72.34705, 72.34705,
72.34705, 72.34705, 72.34705, 72.35019, 72.34705, 72.34705,
72.35019, 72.34705, 72.35019, 72.34705, 72.34705, 72.35019,
72.34705, 72.34705, 72.35019, 72.34705, 72.34705, 72.35019,
72.34705, 72.35019, 72.34705, 72.34705, 72.35019, 72.34705,
72.35019, 72.34705, 72.34705, 72.35019, 72.34705, 72.32009,
72.32009, 72.32195, 72.32195, 72.32195, 72.32195, 72.29368,
72.29111, 72.29368, 72.29368, 72.29368, 72.29368, 72.29368,
72.29111, 72.29368, 72.29111, 72.29368, 72.29111, 72.29368,
72.29111, 72.29368, 72.29111, 72.29368, 72.29111, 72.29368,
72.29111, 72.29111, 72.29111, 72.29368, 72.29111, 72.29368,
72.29111, 72.29111, 72.29368, 72.29111, 72.29111, 72.29111,
72.35019, 72.35019, 72.35019, 72.35019, 72.35019, 72.35019,
72.35019, 72.35019, 72.32544, 72.32544, 72.32738), Longitude = c(-81.09397,
-81.09397, -81.09397, -81.09397, -81.09397, -81.09397, -81.09397,
-81.09397, -81.09397, -81.09397, -81.09397, -81.09397, -81.09397,
-81.09397, -81.09397, -81.09397, -81.09397, -81.09397, -81.08279,
-81.09397, -81.08279, -81.08279, -81.08279, -81.08279, -81.08279,
-81.08279, -81.09397, -81.08279, -81.08279, -81.09397, -81.08279,
-81.09397, -81.08279, -81.08279, -81.09397, -81.08279, -81.08279,
-81.09397, -81.08279, -81.08279, -81.09397, -81.08279, -81.09397,
-81.08279, -81.08279, -81.09397, -81.08279, -81.09397, -81.08279,
-81.08279, -81.09397, -81.08279, -81.11921, -81.11921, -81.13219,
-81.13219, -81.13219, -81.13219, -81.16389, -81.16127, -81.16389,
-81.16389, -81.16389, -81.16389, -81.16389, -81.16127, -81.16389,
-81.16127, -81.16389, -81.16127, -81.16389, -81.16127, -81.16389,
-81.16127, -81.16389, -81.16127, -81.16389, -81.16127, -81.16127,
-81.16127, -81.16389, -81.16127, -81.16389, -81.16127, -81.16127,
-81.16389, -81.16127, -81.16127, -81.16127, -81.09397, -81.09397,
-81.09397, -81.09397, -81.09397, -81.09397, -81.09397, -81.09397,
-81.15897, -81.15897, -81.17314)), row.names = c(41276L,
41277L, 41278L, 41279L, 41280L, 41281L, 41282L, 41283L, 41284L,
41285L, 41286L, 41287L, 41288L, 41289L, 41290L, 41291L, 41292L,
41293L, 41294L, 41295L, 41296L, 41297L, 41298L, 41299L, 41300L,
41301L, 41302L, 41303L, 41304L, 41305L, 41306L, 41307L, 41309L,
41310L, 41311L, 41312L, 41313L, 41314L, 41315L, 41316L, 41317L,
41318L, 41319L, 41320L, 41323L, 41324L, 41326L, 41328L, 41329L,
41330L, 41331L, 41332L, 41432L, 41433L, 41434L, 41436L, 41437L,
41438L, 41441L, 41447L, 41448L, 41452L, 41454L, 41458L, 41460L,
41463L, 41464L, 41465L, 41466L, 41467L, 41469L, 41471L, 41472L,
41474L, 41475L, 41476L, 41477L, 41478L, 41479L, 41480L, 41481L,
41482L, 41483L, 41484L, 41485L, 41486L, 41487L, 41488L, 41489L,
36498L, 36504L, 36510L, 36515L, 36521L, 36524L, 36528L, 36530L,
36765L, 36769L, 36775L), class = "data.frame")
答案 0 :(得分:1)
假设将第二个数据集加载到名为df
的变量中,并且每个Station
显式映射到East
或West
,则可以将每个站编码为分别带有shore
语句的case_when
:
library(tidyverse)
df %>%
mutate(
shore = case_when(
Station %in% c("TRD1", "TRE1", "TRE2") ~ "West",
TRUE ~ "East"
)
) %>%
group_by(ID) %>%
arrange(DateTime) %>%
mutate(
shore_change = shore != lag(shore, 1, default = first(shore))
)
在此链的结尾,我们group_by(ID)
和arrange(DateTime)
说“对于每个ID,按时间顺序排列”,然后创建新变量shore_change
,以检查是否如果 this 支撑不等于(!=)先前的shore
(即lag(shore, 1)
)。 default = first(shore)
可以消除NA
来检查ID的第一个肖氏值。
如果您想获得所有发生这种情况的时间,只需将filter(shore_change == TRUE)
附加到管道的末尾即可。