0

我是 R 新手,所以我希望这个问题足够有趣。我正在处理具有纬度和经度位置以及相关时间和日期的 GPS 位置数据。

以下是数据示例:

> dataraw<-fread("41361_GPS_data_raw.csv", select = c("Date & Time [Local]","Latitude","Longitude"))
> head(dataraw)
   Date & Time [Local]  Latitude Longitude
1:      6/18/2018 3:01 -2.434901  34.85359
2:      6/18/2018 3:06 -2.434598  34.85387
3:      6/18/2018 3:08 -2.434726  34.85382
4:      6/18/2018 3:12 -2.434816  34.85371
5:      6/18/2018 3:16 -2.434613  34.85372
6:      6/18/2018 3:20 -2.434511  34.85376

我想创建一个包含两列“Day”和“Night”的数据框,将一天中的时间划分为“Day”[6:00 到 18:00] 和“Night”[18:00 到 6:00] .

  • “天”列将根据 [6:00 至 18:00] 的 GPS 位置显示总行驶距离(以米为单位)

  • “夜间”列将包含基于 GPS 位置从 [18:00 到 6:00] 行驶的总距离(以米为单位)

我正在使用该包geosphere来计算两个位置之间的距离,但我不确定是否有更有效的函数来计算我的分析。请注意,GPS 位置需要几个月的时间,所以这是一个大数据集。

如果在此之前需要转换数据,也请告诉我。任何输入表示赞赏。

PS 大样本数据

> dput(dataraw)
structure(list(`Date & Time [Local]` = c("18/06/2018 03:01", 
"18/06/2018 03:06", "18/06/2018 03:08", "18/06/2018 03:12", "18/06/2018 03:16", 
"18/06/2018 03:20", "18/06/2018 03:24", "18/06/2018 03:28", "18/06/2018 03:33", 
"18/06/2018 03:36", "18/06/2018 03:40", "18/06/2018 03:45", "18/06/2018 03:48", 
"18/06/2018 03:52", "18/06/2018 03:56", "18/06/2018 04:00", "18/06/2018 04:04", 
"18/06/2018 04:08", "18/06/2018 04:12", "18/06/2018 04:16", "18/06/2018 04:20", 
"18/06/2018 04:24", "18/06/2018 04:28", "18/06/2018 04:32", "18/06/2018 04:36", 
"18/06/2018 04:40", "18/06/2018 04:44", "18/06/2018 04:48", "18/06/2018 04:52", 
"18/06/2018 04:56", "18/06/2018 05:00", "18/06/2018 05:04", "18/06/2018 05:08", 
"18/06/2018 05:12", "18/06/2018 05:16", "18/06/2018 05:20", "18/06/2018 05:24", 
"18/06/2018 05:28", "18/06/2018 05:32", "18/06/2018 05:36", "18/06/2018 05:40", 
"18/06/2018 05:44", "18/06/2018 05:48", "18/06/2018 05:52", "18/06/2018 05:56", 
"18/06/2018 06:00", "18/06/2018 06:05", "18/06/2018 06:08", "18/06/2018 06:12", 
"18/06/2018 06:16", "18/06/2018 06:20", "18/06/2018 06:24", "18/06/2018 06:28", 
"18/06/2018 06:32", "18/06/2018 06:36", "18/06/2018 06:40", "18/06/2018 06:44", 
"18/06/2018 06:48", "18/06/2018 06:52", "18/06/2018 06:57", "18/06/2018 07:01", 
"18/06/2018 07:04", "18/06/2018 07:08", "18/06/2018 07:12", "18/06/2018 07:17", 
"18/06/2018 07:20", "18/06/2018 07:24", "18/06/2018 07:28", "18/06/2018 07:32", 
"18/06/2018 07:36", "18/06/2018 07:40", "18/06/2018 07:44", "18/06/2018 07:48", 
"18/06/2018 07:52", "18/06/2018 07:56", "18/06/2018 08:00", "18/06/2018 08:04", 
"18/06/2018 08:08", "18/06/2018 08:12", "18/06/2018 08:16", "18/06/2018 08:20", 
"18/06/2018 08:24", "18/06/2018 08:28", "18/06/2018 08:32", "18/06/2018 08:36", 
"18/06/2018 08:40", "18/06/2018 08:44", "18/06/2018 08:48", "18/06/2018 08:52", 
"18/06/2018 08:56", "18/06/2018 09:00", "18/06/2018 09:04", "18/06/2018 09:08", 
"18/06/2018 09:12", "18/06/2018 09:16", "18/06/2018 09:20", "18/06/2018 09:24", 
"18/06/2018 09:28", "18/06/2018 09:32", "18/06/2018 09:37", "18/06/2018 09:40", 
"18/06/2018 09:44", "18/06/2018 09:48", "18/06/2018 09:52", "18/06/2018 09:56", 
"18/06/2018 10:00", "18/06/2018 10:05", "18/06/2018 10:08", "18/06/2018 10:12", 
"18/06/2018 10:17", "18/06/2018 10:20", "18/06/2018 10:25", "18/06/2018 10:29", 
"18/06/2018 10:32", "18/06/2018 10:36", "18/06/2018 10:40", "18/06/2018 10:44", 
"18/06/2018 10:48", "18/06/2018 10:52", "18/06/2018 10:56", "18/06/2018 11:01", 
"18/06/2018 11:04", "18/06/2018 11:09", "18/06/2018 11:12", "18/06/2018 11:17", 
"18/06/2018 11:22", "18/06/2018 11:25", "18/06/2018 11:30", "18/06/2018 11:33", 
"18/06/2018 11:36", "18/06/2018 11:41", "18/06/2018 11:46", "18/06/2018 11:49", 
"18/06/2018 11:53", "18/06/2018 11:56", "18/06/2018 12:01", "18/06/2018 12:05", 
"18/06/2018 12:09", "18/06/2018 12:13", "18/06/2018 12:17", "18/06/2018 12:22", 
"18/06/2018 12:27", "18/06/2018 12:29", "18/06/2018 12:33", "18/06/2018 12:38", 
"18/06/2018 12:41", "18/06/2018 12:44", "18/06/2018 12:48", "18/06/2018 12:52", 
"18/06/2018 12:56", "18/06/2018 13:00", "18/06/2018 13:04", "18/06/2018 13:08", 
"18/06/2018 13:12", "18/06/2018 13:16", "18/06/2018 13:20", "18/06/2018 13:24", 
"18/06/2018 13:29", "18/06/2018 13:34", "18/06/2018 13:37", "18/06/2018 13:40", 
"18/06/2018 13:44", "18/06/2018 13:48", "18/06/2018 13:52", "18/06/2018 13:56", 
"18/06/2018 14:00", "18/06/2018 14:04", "18/06/2018 14:08", "18/06/2018 14:12", 
"18/06/2018 14:16", "18/06/2018 14:20", "18/06/2018 14:24", "18/06/2018 14:29", 
"18/06/2018 14:32", "18/06/2018 14:36", "18/06/2018 14:40", "18/06/2018 14:44", 
"18/06/2018 14:48", "18/06/2018 14:52", "18/06/2018 14:56", "18/06/2018 15:01", 
"18/06/2018 15:04", "18/06/2018 15:08", "18/06/2018 15:12", "18/06/2018 15:16", 
"18/06/2018 15:21", "18/06/2018 15:24", "18/06/2018 15:28", "18/06/2018 15:32", 
"18/06/2018 15:36", "18/06/2018 15:40", "18/06/2018 15:44", "18/06/2018 15:48", 
"18/06/2018 15:52", "18/06/2018 15:56", "18/06/2018 16:00", "18/06/2018 16:04", 
"18/06/2018 16:08", "18/06/2018 16:12", "18/06/2018 16:16", "18/06/2018 16:20", 
"18/06/2018 16:24", "18/06/2018 16:28", "18/06/2018 16:32", "18/06/2018 16:36", 
"18/06/2018 16:40", "18/06/2018 16:44", "18/06/2018 16:48", "18/06/2018 16:52", 
"18/06/2018 16:56", "18/06/2018 17:00", "18/06/2018 17:04", "18/06/2018 17:08", 
"18/06/2018 17:12", "18/06/2018 17:16", "18/06/2018 17:20", "18/06/2018 17:24", 
"18/06/2018 17:28", "18/06/2018 17:32", "18/06/2018 17:36", "18/06/2018 17:40", 
"18/06/2018 17:44", "18/06/2018 17:48", "18/06/2018 17:52", "18/06/2018 17:56", 
"18/06/2018 18:00", "18/06/2018 18:04", "18/06/2018 18:08", "18/06/2018 18:12", 
"18/06/2018 18:16", "18/06/2018 18:20", "18/06/2018 18:24", "18/06/2018 18:28", 
"18/06/2018 18:32", "18/06/2018 18:36", "18/06/2018 18:40", "18/06/2018 18:44", 
"18/06/2018 18:48", "18/06/2018 18:52", "18/06/2018 18:56", "18/06/2018 19:00", 
"18/06/2018 19:04", "18/06/2018 19:08", "18/06/2018 19:12", "18/06/2018 19:16", 
"18/06/2018 19:20", "18/06/2018 19:24", "18/06/2018 19:28", "18/06/2018 19:32", 
"18/06/2018 19:36", "18/06/2018 19:40", "18/06/2018 19:44", "18/06/2018 19:48", 
"18/06/2018 19:52", "18/06/2018 19:56", "18/06/2018 20:00", "18/06/2018 20:04", 
"18/06/2018 20:08", "18/06/2018 20:12", "18/06/2018 20:16", "18/06/2018 20:20", 
"18/06/2018 20:24", "18/06/2018 20:28", "18/06/2018 20:32", "18/06/2018 20:36", 
"18/06/2018 20:40", "18/06/2018 20:44", "18/06/2018 20:48", "18/06/2018 20:52", 
"18/06/2018 20:56", "18/06/2018 21:00", "18/06/2018 21:04", "18/06/2018 21:08", 
"18/06/2018 21:12", "18/06/2018 21:16", "18/06/2018 21:20", "18/06/2018 21:24", 
"18/06/2018 21:28", "18/06/2018 21:32", "18/06/2018 21:36", "18/06/2018 21:40", 
"18/06/2018 21:44", "18/06/2018 21:48", "18/06/2018 21:52", "18/06/2018 21:56", 
"18/06/2018 22:00", "18/06/2018 22:04", "18/06/2018 22:08", "18/06/2018 22:12", 
"18/06/2018 22:16", "18/06/2018 22:20", "18/06/2018 22:24", "18/06/2018 22:28", 
"18/06/2018 22:32", "18/06/2018 22:36", "18/06/2018 22:40", "18/06/2018 22:44", 
"18/06/2018 22:48", "18/06/2018 22:52", "18/06/2018 22:56", "18/06/2018 23:00", 
"18/06/2018 23:04", "18/06/2018 23:08", "18/06/2018 23:12", "18/06/2018 23:16", 
"18/06/2018 23:20", "18/06/2018 23:24", "18/06/2018 23:28", "18/06/2018 23:32", 
"18/06/2018 23:36", "18/06/2018 23:40", "18/06/2018 23:44", "18/06/2018 23:48", 
"18/06/2018 23:52", "18/06/2018 23:56", "19/06/2018 00:00", "19/06/2018 00:04", 
"19/06/2018 00:08", "19/06/2018 00:12", "19/06/2018 00:16", "19/06/2018 00:20", 
"19/06/2018 00:24", "19/06/2018 00:28", "19/06/2018 00:32", "19/06/2018 00:36", 
"19/06/2018 00:40", "19/06/2018 00:44", "19/06/2018 00:48", "19/06/2018 00:52", 
"19/06/2018 00:56", "19/06/2018 01:00", "19/06/2018 01:04", "19/06/2018 01:08", 
"19/06/2018 01:12", "19/06/2018 01:16", "19/06/2018 01:20", "19/06/2018 01:24", 
"19/06/2018 01:28", "19/06/2018 01:32", "19/06/2018 01:36", "19/06/2018 01:40"
), Latitude = c(-2.4349008, -2.4345981, -2.4347258, -2.4348164, 
-2.4346131, -2.4345111, -2.4345033, -2.4347186, -2.4347181, -2.4348167, 
-2.4346744, -2.434745, -2.4346639, -2.4346708, -2.4346603, -2.4346686, 
-2.4346839, -2.4346917, -2.4346292, -2.4346547, -2.4347036, -2.4346725, 
-2.4346589, -2.4346833, -2.434695, -2.4346175, -2.4345903, -2.4346786, 
-2.4346525, -2.434615, -2.4346125, -2.4345792, -2.4345994, -2.4346414, 
-2.4346417, -2.4346753, -2.4346939, -2.4346256, -2.4346519, -2.4346192, 
-2.4346089, -2.4346731, -2.4347681, -2.4347239, -2.4346814, -2.4347231, 
-2.4346, -2.4346792, -2.4346072, -2.4346389, -2.4346383, -2.434605, 
-2.4347006, -2.4347186, -2.4346558, -2.4346703, -2.4346744, -2.4346983, 
-2.4348144, -2.4344161, -2.43485, -2.4348183, -2.4346917, -2.4346072, 
-2.43465, -2.4345392, -2.4346864, -2.4346872, -2.4346269, -2.4345764, 
-2.4346433, -2.4346817, -2.4346928, -2.4346631, -2.4346525, -2.4348539, 
-2.4346383, -2.4345847, -2.4345206, -2.4347272, -2.4346569, -2.4345236, 
-2.4346842, -2.4347886, -2.4346625, -2.4346364, -2.4346333, -2.4345917, 
-2.4346356, -2.4346767, -2.4346158, -2.4346472, -2.4346192, -2.4347022, 
-2.4346811, -2.4346297, -2.4346111, -2.4346622, -2.4346675, NA, 
-2.4346772, -2.4346939, -2.4346461, -2.4346019, -2.4346139, -2.4348478, 
-2.4347394, -2.4346625, -2.4346439, -2.4346794, -2.4347111, -2.434585, 
-2.4347006, -2.4347433, -2.4346411, -2.4347028, -2.4346817, -2.4347439, 
-2.4346769, -2.4346361, -2.4346717, -2.4346219, -2.4347819, -2.4347103, 
-2.4347506, -2.4346897, -2.4346925, -2.4346978, -2.4346964, -2.4347219, 
-2.4347267, -2.4345564, -2.4346508, -2.4346486, -2.4346811, -2.4350631, 
-2.4348117, -2.4347906, -2.4347039, -2.4349119, -2.4347797, NA, 
-2.4346444, NA, -2.4346842, -2.4346911, -2.4347128, -2.4347997, 
-2.4347008, -2.4347275, -2.4347192, -2.434735, -2.4345725, -2.4347472, 
-2.4346994, -2.4346981, -2.4346889, -2.4345267, -2.4347344, -2.4346525, 
-2.4345986, -2.4346194, -2.43469, -2.434695, -2.4346586, -2.4346967, 
-2.4346347, -2.4346678, -2.4346183, -2.4346778, -2.4346742, -2.4346319, 
-2.4346044, -2.4347036, -2.4347056, -2.4345906, -2.4345814, -2.4347161, 
-2.4347058, -2.4349583, -2.4344272, -2.4402072, -2.4306572, -2.4384197, 
-2.4377283, -2.4367728, -2.4369347, -2.4367983, -2.4365889, -2.4386911, 
-2.4384722, -2.4377939, -2.4377661, -2.4374156, -2.4374167, -2.4381381, 
-2.4382022, -2.4383194, -2.4382017, -2.4379392, -2.4379406, -2.4374514, 
-2.43689, -2.4369081, -2.4367461, -2.4365292, -2.4364558, -2.4364569, 
-2.4364564, -2.4364558, -2.4364467, -2.4367092, -2.4366642, -2.4366719, 
-2.4366094, -2.4366461, -2.4366547, -2.4366547, -2.4367356, -2.4366725, 
-2.4366728, -2.4367275, -2.4368083, -2.4369419, -2.436935, -2.4369522, 
-2.4370344, -2.4371542, -2.4370969, -2.4370978, -2.4370972, -2.4370983, 
-2.4370972, -2.4370894, -2.4370972, -2.4373953, -2.4373425, -2.4373597, 
-2.4373503, -2.4373517, -2.4373506, -2.4373431, -2.4373428, -2.4373431, 
-2.4373442, -2.4373333, -2.4373439, -2.4373422, -2.4373614, -2.4373433, 
-2.4373331, -2.4373436, -2.4373603, -2.4373436, -2.4372525, -2.4371892, 
-2.4371886, -2.4372075, -2.4372344, -2.4372058, -2.4372344, -2.4372439, 
-2.4372525, -2.4372342, -2.4372433, -2.4372525, -2.4372519, -2.4372439, 
-2.4372522, -2.4372519, -2.4372356, -2.4372356, -2.4372447, -2.4372453, 
-2.4372536, -2.4372531, -2.4372447, -2.4372467, -2.437235, -2.4372431, 
-2.4372508, -2.4372344, -2.4372344, -2.4372428, -2.4372519, -2.4373531, 
-2.4373708, -2.4373522, -2.4373878, -2.4373611, -2.4373336, -2.4373125, 
-2.4373522, -2.4373522, -2.4373433, -2.4373339, -2.4373525, -2.4373525, 
-2.4373433, -2.4373442, -2.4373519, -2.4373417, -2.4373597, -2.4373533, 
-2.4373528, -2.4373517, -2.4373508, -2.4373539, -2.4373519, -2.4373444, 
-2.4373531, -2.4373614, -2.4373619, -2.4373531, -2.4373458, -2.4373525, 
-2.4373606, -2.4373453, -2.4373531, -2.4369633, -2.4366472, -2.4366381, 
-2.4366467, -2.4363933, -2.4363844, -2.4363481, -2.4363572, -2.4363397, 
-2.4362944, -2.4363042, -2.4362858, -2.4363575, -2.4365206, -2.4365833, 
-2.4365389, -2.4365572, -2.4364672, -2.4365031, -2.4365206, -2.4365294, 
-2.4365119), Longitude = c(34.8535933, 34.8538736, 34.8538225, 
34.8537108, 34.853725, 34.8537628, 34.8539311, 34.8538208, 34.8537458, 
34.8537736, 34.8537714, 34.8538236, 34.8537781, 34.8538139, 34.8538369, 
34.8537836, 34.8537567, 34.8537628, 34.8538817, 34.8537781, 34.8537881, 
34.8537928, 34.8538686, 34.8537789, 34.85382, 34.85377, 34.8538406, 
34.8536922, 34.8537619, 34.8537433, 34.853785, 34.8536892, 34.85379, 
34.853875, 34.8537903, 34.8537886, 34.8536175, 34.8536267, 34.8538233, 
34.85379, 34.8538144, 34.8537781, 34.8538086, 34.8538706, 34.8537481, 
34.8538278, 34.85389, 34.8536939, 34.8537694, 34.8537817, 34.8538486, 
34.8538756, 34.8536661, 34.8536986, 34.8538383, 34.8537569, 34.8537589, 
34.853695, 34.853625, 34.8537925, 34.8537064, 34.8539625, 34.8537236, 
34.8536778, 34.8538181, 34.8537122, 34.8537711, 34.8538261, 34.8537553, 
34.85381, 34.8537189, 34.8537731, 34.8538992, 34.8537628, 34.8538144, 
34.8543881, 34.8536436, 34.8536719, 34.8535753, 34.8536933, 34.8538072, 
34.8537278, 34.8536269, 34.8537075, 34.8537575, 34.8537406, 34.8537147, 
34.8538469, 34.8537575, 34.85373, 34.8539133, 34.8537914, 34.8537508, 
34.8537761, 34.8537878, 34.8537767, 34.8539447, 34.8537992, 34.8537947, 
NA, 34.8537783, 34.8536497, 34.8538356, 34.8539083, 34.853825, 
34.8537714, 34.8537506, 34.8538225, 34.8537206, 34.8538144, 34.8537892, 
34.853825, 34.8537111, 34.8537472, 34.8537642, 34.8537794, 34.8537856, 
34.85377, 34.8537733, 34.8537356, 34.8536928, 34.8536931, 34.85388, 
34.8536497, 34.8537939, 34.85379, 34.8537389, 34.8536917, 34.8537614, 
34.8536833, 34.8535286, 34.8537486, 34.8537436, 34.8538033, 34.8537428, 
34.8542131, 34.8537464, 34.8536161, 34.8536528, 34.8537589, 34.8537369, 
NA, 34.8538242, NA, 34.8538142, 34.8538356, 34.853775, 34.8537578, 
34.8537811, 34.8538083, 34.8536906, 34.8537503, 34.8538486, 34.8537175, 
34.8538453, 34.8536575, 34.8537547, 34.8537511, 34.8537075, 34.8537744, 
34.8537633, 34.85359, 34.8537122, 34.8538506, 34.8537231, 34.8537228, 
34.8537347, 34.8537283, 34.8538769, 34.8537114, 34.8538169, 34.8537208, 
34.8536908, 34.8538978, 34.8538761, 34.8539753, 34.8539053, 34.8538122, 
34.8538114, 34.8537864, 34.853695, 34.8503172, 34.8304297, 34.814945, 
34.8145011, 34.8138214, 34.8136814, 34.8137883, 34.8139769, 34.8122244, 
34.81164, 34.8114103, 34.8117689, 34.8128397, 34.8128475, 34.8134156, 
34.8135281, 34.8136578, 34.8137497, 34.8139517, 34.8139722, 34.8141908, 
34.8135119, 34.8135247, 34.81347, 34.8136194, 34.81427, 34.8142653, 
34.8142803, 34.81427, 34.81427, 34.8144422, 34.8145519, 34.8145564, 
34.8149731, 34.8150806, 34.8150828, 34.8150828, 34.8159381, 34.8162597, 
34.8163619, 34.8164819, 34.8165492, 34.8167053, 34.8166839, 34.8166936, 
34.8166964, 34.8171572, 34.817145, 34.8171353, 34.8171303, 34.8171256, 
34.8171375, 34.8171381, 34.8171303, 34.8176861, 34.8177839, 34.8178061, 
34.8178208, 34.8177892, 34.8178011, 34.8177817, 34.8177892, 34.8177744, 
34.81777, 34.8177964, 34.8177847, 34.8177714, 34.8177869, 34.8177869, 
34.8177789, 34.8177722, 34.8177914, 34.8177794, 34.8179564, 34.8180086, 
34.8180233, 34.8179689, 34.8179114, 34.8178533, 34.8178789, 34.8178642, 
34.8178664, 34.8178739, 34.8178664, 34.8178664, 34.8178686, 34.8178642, 
34.8178864, 34.8178686, 34.8178619, 34.8178547, 34.8178672, 34.8178775, 
34.8178944, 34.8178642, 34.8178869, 34.8179103, 34.8178444, 34.8178489, 
34.8178733, 34.8178664, 34.8178914, 34.8178761, 34.8178489, 34.8177897, 
34.8177847, 34.8177619, 34.8177964, 34.8178067, 34.8177892, 34.8177828, 
34.8177869, 34.8177744, 34.8177869, 34.8177817, 34.8177794, 34.8177794, 
34.8177869, 34.8177897, 34.8177817, 34.8177811, 34.8178136, 34.8177825, 
34.8177722, 34.8177892, 34.8177864, 34.8177675, 34.8177692, 34.8177825, 
34.8177772, 34.8177994, 34.8177772, 34.8177897, 34.8177308, 34.8177669, 
34.8177517, 34.8177853, 34.8177647, 34.8174078, 34.8170983, 34.8170858, 
34.8170683, 34.8172375, 34.8172303, 34.8172375, 34.817295, 34.8173003, 
34.8173325, 34.8173753, 34.8174275, 34.8175919, 34.8178217, 34.8177339, 
34.8176794, 34.8176844, 34.8177919, 34.8178267, 34.8178614, 34.8178489, 
34.8178267)), row.names = c(NA, -341L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000000002531ef0>)
4

1 回答 1

1
library("lubridate")
library("geosphere")
library("data.table")

# You probably don't need to create a copy of your own data.
# I did this to avoid an "Invalid .internal.selfref" error from data.table.
dt <- copy(dataraw)

# Preprocess datatimes. Be careful about the format.
# Note that "6/18/2018" must be mdy (month,day,year)
# while "18/06/2018" must be dmy (day,month,year).
# So `dmy_hm` means (day,month,year) followed by (hour,minute).
dt[, DateTime := dmy_hm(`Date & Time [Local]`)]

# Aside:
# The data has NA's in the Latitude/Longitude columns.
# Have you decided how to handle those?

dt[, meters := distGeo(
  cbind(Longitude, Latitude),
  cbind(
    shift(Longitude, 1, type = "lead"),
    shift(Latitude, 1, type = "lead")
  )
)]
dt[, date := date(DateTime)]
dt[, hour := ifelse((hour(DateTime) >= 6) &
                      (hour(DateTime) < 18), "Day", "Night")]
dt[, .(dist = sum(meters, na.rm = TRUE)), .(date, hour)]
#>          date  hour      dist
#> 1: 2018-06-18 Night  880.9310
#> 2: 2018-06-18   Day 8775.9969
#> 3: 2018-06-19 Night  288.1382

reprex 包(v0.2.1)于 2019 年 3 月 11 日创建

以前dplyr的解决方案。

library("lubridate")
library("geosphere")
library("tidyverse")

df <- tribble(
  ~DateTime, ~Latitude, ~Longitude,
  "6/18/2018 3:01", -2.434901,  34.85359,
  "6/18/2018 3:06", -2.434598,  34.85387,
  "6/18/2018 3:08", -2.434726,  34.85382,
  "6/18/2018 3:12", -2.434816,  34.85371,
  "6/18/2018 3:16", -2.434613,  34.85372,
  "6/18/2018 3:20", -2.434511,  34.85376
) %>%
  # Transform string to date/time
  mutate(DateTime = mdy_hm(DateTime))

df %>%
  # Compute the distance between this row and the next one
  mutate(meters = distGeo(cbind(Longitude, Latitude),
                          cbind(lead(Longitude), lead(Latitude)))) %>%
  # Extract the date and the hour, label hours are Day or Night
  mutate(date = date(DateTime),
         hour = if_else((hour(DateTime) >= 6) &
                        (hour(DateTime) < 18), "Day", "Night")) %>%
  # Sum distances by date and Day/Night
  group_by(date, hour) %>%
  summarise(dist = sum(meters, na.rm = TRUE))
#> # A tibble: 1 x 3
#> # Groups:   date [1]
#>   date       hour   dist
#>   <date>     <chr> <dbl>
#> 1 2018-06-18 Night  111.
于 2019-03-11T14:52:10.080 回答