gpt4 book ai didi

r - 不规则时间序列数据 - 我可以让它规则吗?在 r

转载 作者:行者123 更新时间:2023-12-04 06:21:55 27 4
gpt4 key购买 nike

我的数据被编程为每 5 小时获取一次信息,这意味着每天有多个数据点。问题有时是数据记录器出现故障或电池没电或其他任何原因,并且数据丢失或尝试失败。

我所做的所有分析都是基于这些日期。我需要根据此日期/时间戳对所有内容进行排序和过滤并选择所有内容。截至目前,我正在通过“冗长的”脚本来选择和子集我的数据。另外我有几个数据记录器(个人)所以我也是批处理。例如,我想每天只使用一个数据点进行子集化和使用

    #Subset data by minimum time interval
SubsetData = T
MinInterval = 47 #minimum time interval (in hours) between consecutive locations
###########
##Setting the date and time to be recognized as date time
loc$DateTime = as.POSIXct(strptime(loc$DT,"%m/%d/%Y %H:%M",tz="UTC"),tz="UTC")
### Code to subset data based on minimum time interval between locations
if (SubsetData ==T){
row = 0
EndLoc = 2
SubData1 = loc[1,]
IDNames = levels(loc$ID)
for (n in 1:length(IDNames)){
IndivData = loc[loc$ID==IDNames[n],]
row = row+1
SubData1[row,]=IndivData[1,]
while (EndLoc<nrow(IndivData) ){
timediff = difftime(IndivData$DateTime[EndLoc],SubData1$DateTime[row],units = "hours")
if (timediff>MinInterval){
row = row+1
SubData1=rbind(SubData1,IndivData[EndLoc,])
EndLoc = EndLoc+1
} else{
EndLoc = EndLoc+1
}
} #end while loop
} #end loop through individuals
loc = SubData1
} #end conditional to subset data
}
### End Code to subset data'

那么,有没有办法让我的数据变成时间序列数据,让操作更容易呢?我看过关于 Zoo package a xts 的讨论 - 但这些似乎要求数据在规则的时间序列中。我什至不得不不使用某些脚本,因为它们要求数据采用 ts 格式,而我不知道如何将我的数据转换为这种漂亮的格式。有没有好的资源可以教自己这个?我什至应该使用 ts 吗?

以下是我的数据示例:
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c_002",
"c_402"), class = "factor"), Long = c(-147.635233, -147.635232,
-147.63632, -147.636136, -147.63628, -147.635743, -147.631785,
-147.619598, -147.605088, -147.602119, -147.179737, -147.133275,
-147.106584, -147.033649, -147.016078, -147.031872, -146.940969,
-146.862538, -146.751214, -146.696238, -146.710799), Latitude = c(67.911827,
67.911193, 67.911048, 67.911169, 67.911625, 67.911767, 67.919419,
67.926327, 67.925352, 67.926318, 70.017026, 70.041163, 70.057988,
70.087812, 70.117134, 70.147184, 70.145115, 70.136103, 70.1276,
70.103395, 70.079301), DT = structure(c(2L, 3L, 5L, 6L, 8L, 9L,
10L, 11L, 12L, 14L, 393L, 395L, 396L, 398L, 399L, 401L, 402L,
404L, 405L, 407L, 409L), .Label = c("05/01/2006 00:00", "05/01/2006 00:01",
"05/01/2006 05:00", "05/01/2006 05:01", "05/01/2006 10:00", "05/01/2006 15:01",
"05/01/2006 20:00", "05/01/2006 20:01", "05/02/2006 01:00", "05/02/2006 06:00",
"05/02/2006 11:00", "05/02/2006 16:01", "05/02/2006 21:00", "05/02/2006 21:01",
"05/03/2006 02:00", "05/03/2006 02:01", "05/03/2006 07:00", "05/03/2006 07:01",
"05/03/2006 12:00", "05/03/2006 17:00", "05/03/2006 17:01", "05/03/2006 22:00",
"05/03/2006 22:01", "05/04/2006 03:00", "05/04/2006 03:01", "05/04/2006 08:00",
"05/04/2006 08:01", "05/04/2006 13:00", "05/04/2006 18:00", "05/04/2006 23:00",
"05/04/2006 23:01", "05/05/2006 04:00", "05/05/2006 09:00", "05/05/2006 09:01",
"05/05/2006 14:00", "05/05/2006 19:00", "05/06/2006 00:00", "05/06/2006 00:01",
"05/06/2006 05:00", "05/06/2006 05:01", "05/06/2006 10:00", "05/06/2006 10:01",
"05/06/2006 15:01", "05/06/2006 20:00", "05/06/2006 20:01", "05/07/2006 01:00",
"05/07/2006 01:01", "05/07/2006 06:00", "05/07/2006 06:01", "05/07/2006 11:00",
"05/07/2006 16:00", "05/07/2006 16:01", "05/07/2006 21:00", "05/08/2006 02:00",
"05/08/2006 07:01", "05/08/2006 12:00", "05/08/2006 17:00", "05/08/2006 17:01",
"05/08/2006 22:00", "05/08/2006 22:01", "05/09/2006 03:00", "05/09/2006 03:01",
"05/09/2006 08:00", "05/09/2006 08:01", "05/09/2006 13:00", "05/09/2006 13:01",
"05/09/2006 18:00", "05/09/2006 23:00", "05/09/2006 23:01", "05/10/2006 04:00",
"05/10/2006 04:01", "05/10/2006 09:00", "05/10/2006 09:01", "05/10/2006 14:00",
"05/10/2006 14:01", "05/10/2006 19:00", "05/10/2006 19:03", "05/11/2006 00:00",
"05/11/2006 05:00", "05/11/2006 10:00", "05/11/2006 10:01", "05/11/2006 15:01",
"05/11/2006 15:02", "05/11/2006 20:00", "05/11/2006 20:01", "05/12/2006 01:00",
"05/12/2006 01:01", "05/12/2006 06:00", "05/12/2006 11:00", "05/12/2006 16:00",
"05/12/2006 16:01", "05/12/2006 21:00", "05/12/2006 21:01", "05/13/2006 02:00",
"05/13/2006 07:00", "05/13/2006 07:01", "05/13/2006 12:00", "05/13/2006 17:00",
"05/13/2006 22:00", "05/13/2006 22:01", "05/14/2006 03:00", "05/14/2006 08:00",
"05/14/2006 13:00", "05/14/2006 18:00", "05/14/2006 18:01", "05/14/2006 23:00",
"05/15/2006 04:00", "05/15/2006 04:01", "05/15/2006 09:00", "05/15/2006 09:03",
"05/15/2006 14:00", "05/15/2006 19:00", "05/16/2006 00:00", "05/16/2006 05:01",
"05/16/2006 10:00", "05/16/2006 15:00", "05/16/2006 15:01", "05/16/2006 20:00",
"05/17/2006 01:01", "05/17/2006 01:02", "05/17/2006 06:01", "05/17/2006 11:00",
"05/17/2006 11:01", "05/17/2006 16:00", "05/17/2006 21:00", "05/18/2006 02:00",
"05/18/2006 02:01", "05/18/2006 07:00", "05/18/2006 07:01", "05/18/2006 12:00",
"05/18/2006 12:01", "05/18/2006 17:00", "05/18/2006 22:00", "05/18/2006 22:01",
"05/19/2006 03:00", "05/19/2006 08:00", "05/19/2006 13:01", "05/19/2006 18:00",
"05/19/2006 23:00", "05/19/2006 23:01", "05/20/2006 04:00", "05/20/2006 04:01",
"05/20/2006 09:00", "05/20/2006 09:01", "05/20/2006 14:00", "05/20/2006 14:01",
"05/20/2006 19:00", "05/21/2006 00:01", "05/21/2006 00:02", "05/21/2006 05:00",
"05/21/2006 10:00", "05/21/2006 15:00", "05/21/2006 15:01", "05/21/2006 20:00",
"05/22/2006 01:01", "05/22/2006 06:00", "05/22/2006 11:00", "05/22/2006 11:01",
"05/22/2006 16:00", "05/22/2006 16:01", "05/22/2006 21:00", "05/22/2006 21:01",
"05/23/2006 02:00", "05/23/2006 07:00", "05/23/2006 07:01", "05/23/2006 12:00",
"05/23/2006 17:01", "05/23/2006 22:00", "05/24/2006 03:00", "05/24/2006 08:00",
"05/24/2006 08:01", "05/24/2006 13:00", "05/24/2006 18:00", "05/24/2006 18:01",
"05/24/2006 23:00", "05/24/2006 23:01", "05/25/2006 04:01", "05/25/2006 09:00",
"05/25/2006 09:02", "05/25/2006 14:01", "05/25/2006 19:01", "05/26/2006 00:01",
"05/26/2006 05:00", "05/26/2006 10:00", "05/26/2006 15:00", "05/26/2006 20:00",
"05/27/2006 01:00", "05/27/2006 01:01", "05/27/2006 06:00", "05/27/2006 11:00",
"05/27/2006 16:00", "05/27/2006 16:01", "05/27/2006 21:01", "05/28/2006 02:00",
"05/28/2006 02:01", "05/28/2006 07:00", "05/28/2006 07:01", "05/28/2006 12:01",
"05/28/2006 17:00", "05/28/2006 22:00", "05/29/2006 03:00", "05/29/2006 03:01",
"05/29/2006 08:00", "05/29/2006 13:00", "05/29/2006 13:01", "05/29/2006 18:00",
"05/29/2006 23:01", "05/30/2006 04:00", "05/30/2006 09:00", "05/30/2006 14:00",
"05/30/2006 14:01", "05/30/2006 19:00", "05/30/2006 19:01", "05/31/2006 00:00",
"05/31/2006 00:01", "05/31/2006 05:01", "05/31/2006 10:00", "05/31/2006 10:01",
"05/31/2006 15:00", "05/31/2006 15:01", "05/31/2006 20:00", "06/01/2006 01:00",
"06/01/2006 01:01", "06/01/2006 06:00", "06/01/2006 06:01", "06/01/2006 11:00",
"06/01/2006 11:01", "06/01/2006 16:00", "06/01/2006 21:01", "06/02/2006 02:01",
"06/02/2006 07:00", "06/02/2006 12:00", "06/02/2006 12:01", "06/02/2006 17:00",
"06/02/2006 17:02", "06/02/2006 22:01", "06/03/2006 03:00", "06/03/2006 03:01",
"06/03/2006 08:00", "06/03/2006 08:01", "06/03/2006 13:00", "06/03/2006 13:01",
"06/03/2006 18:00", "06/03/2006 23:00", "06/04/2006 04:00", "06/04/2006 04:01",
"06/04/2006 09:00", "06/04/2006 09:01", "06/04/2006 14:00", "06/04/2006 19:00",
"06/05/2006 00:00", "06/05/2006 05:01", "06/05/2006 10:00", "06/05/2006 15:00",
"06/05/2006 20:00", "06/06/2006 01:00", "06/06/2006 01:01", "06/06/2006 06:01",
"06/06/2006 11:00", "06/06/2006 11:01", "06/06/2006 16:00", "06/06/2006 21:00",
"06/06/2006 21:01", "06/07/2006 02:01", "06/07/2006 07:00", "06/07/2006 07:01",
"06/07/2006 12:00", "06/07/2006 12:01", "06/07/2006 17:00", "06/07/2006 17:01",
"06/07/2006 22:00", "06/07/2006 22:01", "06/08/2006 03:00", "06/08/2006 08:00",
"06/08/2006 08:01", "06/08/2006 13:00", "06/08/2006 13:01", "06/08/2006 18:00",
"06/08/2006 23:00", "06/09/2006 04:00", "06/09/2006 09:00", "06/09/2006 14:00",
"06/09/2006 19:00", "06/10/2006 00:01", "06/10/2006 00:02", "06/10/2006 05:00",
"06/10/2006 10:00", "06/10/2006 15:00", "06/10/2006 20:01", "06/11/2006 01:00",
"06/11/2006 01:01", "06/11/2006 06:00", "06/11/2006 11:00", "06/11/2006 16:00",
"06/11/2006 16:01", "06/11/2006 21:00", "06/12/2006 02:01", "06/12/2006 02:02",
"06/12/2006 07:00", "06/12/2006 07:01", "06/12/2006 12:00", "06/12/2006 17:00",
"06/12/2006 17:01", "06/12/2006 22:00", "06/12/2006 22:01", "06/13/2006 03:00",
"06/13/2006 03:01", "06/13/2006 08:00", "06/13/2006 08:01", "06/13/2006 13:00",
"06/13/2006 13:03", "06/13/2006 18:00", "06/13/2006 18:01", "06/13/2006 23:00",
"06/13/2006 23:01", "06/14/2006 04:00", "06/14/2006 04:01", "06/14/2006 09:00",
"06/14/2006 09:01", "06/14/2006 14:00", "06/14/2006 14:01", "06/14/2006 19:00",
"06/15/2006 00:00", "06/15/2006 05:01", "06/15/2006 10:00", "06/15/2006 15:00",
"06/15/2006 15:01", "06/15/2006 20:00", "06/15/2006 20:01", "06/16/2006 01:01",
"06/16/2006 06:00", "06/16/2006 06:01", "06/16/2006 11:00", "06/16/2006 11:01",
"06/16/2006 16:00", "06/16/2006 16:01", "06/16/2006 21:00", "06/16/2006 21:01",
"06/17/2006 02:00", "06/17/2006 07:00", "06/17/2006 07:01", "06/17/2006 12:00",
"06/17/2006 12:01", "06/17/2006 17:00", "06/17/2006 22:00", "06/18/2006 03:00",
"06/18/2006 03:01", "06/18/2006 08:01", "06/18/2006 13:00", "06/18/2006 13:01",
"06/18/2006 18:01", "06/18/2006 23:00", "06/19/2006 04:00", "06/19/2006 04:01",
"06/19/2006 09:00", "06/19/2006 09:01", "06/19/2006 14:01", "06/19/2006 19:01",
"06/20/2006 00:00", "06/20/2006 05:00", "06/20/2006 05:01", "06/20/2006 10:01",
"06/20/2006 15:00", "06/20/2006 15:01", "06/20/2006 20:00", "06/21/2006 01:00",
"06/21/2006 01:01", "06/21/2006 06:00", "06/21/2006 06:01", "06/21/2006 11:01",
"06/21/2006 16:00", "06/21/2006 16:01", "06/21/2006 21:00", "06/21/2006 21:01",
"06/22/2006 02:00", "06/22/2006 02:02", "06/22/2006 07:00", "06/22/2006 12:00",
"06/22/2006 12:01", "06/22/2006 17:00", "06/22/2006 17:01", "06/22/2006 22:00",
"06/23/2006 03:00", "06/23/2006 08:01", "06/23/2006 13:00", "06/23/2006 13:03",
"06/23/2006 18:01", "06/23/2006 18:02", "06/23/2006 23:00", "06/24/2006 04:00",
"06/24/2006 09:01", "06/24/2006 14:00", "06/24/2006 19:00", "06/24/2006 19:01",
"06/25/2006 00:01", "06/25/2006 05:00", "06/25/2006 10:00", "06/25/2006 10:01",
"06/25/2006 15:00", "06/25/2006 15:01", "06/25/2006 20:00", "06/26/2006 01:00",
"06/26/2006 01:01", "06/26/2006 06:00", "06/26/2006 11:00", "06/26/2006 11:01",
"06/26/2006 16:00", "06/26/2006 21:00", "06/26/2006 21:01", "06/27/2006 02:00",
"06/27/2006 07:01", "06/27/2006 12:00", "06/27/2006 17:01", "06/27/2006 22:00",
"06/28/2006 03:00", "06/28/2006 08:00", "06/28/2006 08:01", "06/28/2006 13:00",
"06/28/2006 18:00", "06/28/2006 23:00", "06/28/2006 23:01", "06/29/2006 04:00",
"06/29/2006 09:01", "06/29/2006 14:00", "06/29/2006 19:01", "06/30/2006 00:00",
"06/30/2006 00:02", "06/30/2006 05:01", "06/30/2006 10:00", "06/30/2006 10:01",
"06/30/2006 15:00", "06/30/2006 20:00", "07/01/2006 01:00", "07/01/2006 06:00",
"07/01/2006 06:01", "07/01/2006 11:00", "07/01/2006 11:01", "07/01/2006 16:00",
"07/01/2006 21:00", "07/01/2006 21:01", "07/02/2006 02:00", "07/02/2006 07:00",
"07/02/2006 12:00", "07/02/2006 17:01", "07/02/2006 22:00", "07/03/2006 03:00",
"07/03/2006 03:01", "07/03/2006 08:00", "07/03/2006 13:00", "07/03/2006 18:01",
"07/03/2006 23:00", "07/03/2006 23:01", "07/04/2006 04:00", "07/04/2006 09:00",
"07/04/2006 09:01", "07/04/2006 14:00", "07/04/2006 14:01", "07/04/2006 19:00",
"07/04/2006 19:01", "07/05/2006 00:00", "07/05/2006 05:00", "07/05/2006 05:01",
"07/05/2006 10:01", "07/05/2006 15:00", "07/05/2006 20:00", "07/06/2006 01:00",
"07/06/2006 01:01", "07/06/2006 06:00", "07/06/2006 06:01", "07/06/2006 11:00",
"07/06/2006 11:01", "07/06/2006 16:00", "07/06/2006 21:00", "07/06/2006 21:01",
"07/07/2006 02:00", "07/07/2006 07:00", "07/07/2006 12:01", "07/07/2006 17:00",
"07/07/2006 22:01", "07/08/2006 03:00", "07/08/2006 08:01", "07/08/2006 13:01",
"07/08/2006 18:00", "07/08/2006 18:01", "07/08/2006 23:01", "07/09/2006 04:00",
"07/09/2006 09:00", "07/09/2006 14:00", "07/09/2006 19:01", "07/10/2006 00:01",
"07/10/2006 05:00", "07/10/2006 10:01", "07/10/2006 15:00", "07/10/2006 15:01",
"07/10/2006 20:00", "07/11/2006 01:00", "07/11/2006 01:01", "07/11/2006 06:00",
"07/11/2006 06:01", "07/11/2006 11:00", "07/11/2006 11:01", "07/11/2006 16:00",
"07/11/2006 21:00", "07/12/2006 02:00", "07/12/2006 02:01", "07/12/2006 07:01",
"07/12/2006 12:00", "07/12/2006 12:01", "07/12/2006 17:00", "07/12/2006 17:01",
"07/12/2006 22:00", "07/12/2006 22:01", "07/13/2006 03:01", "07/13/2006 08:01",
"07/13/2006 08:02", "07/13/2006 13:00", "07/13/2006 13:01", "07/13/2006 18:01",
"07/13/2006 23:01", "07/14/2006 04:00", "07/14/2006 04:01", "07/14/2006 09:00",
"07/14/2006 09:01", "07/14/2006 14:00", "07/14/2006 14:01", "07/14/2006 19:00",
"07/14/2006 19:01", "07/15/2006 00:00", "07/15/2006 00:01", "07/15/2006 05:00",
"07/15/2006 10:01", "07/15/2006 15:00", "07/15/2006 15:01", "07/15/2006 20:02",
"07/15/2006 20:03", "07/16/2006 01:00", "07/16/2006 01:01", "07/16/2006 06:00",
"07/16/2006 06:01", "07/16/2006 11:00", "07/16/2006 11:03", "07/16/2006 16:01",
"07/16/2006 21:00", "07/16/2006 21:02", "07/17/2006 02:00", "07/17/2006 02:01",
"07/17/2006 07:00", "07/17/2006 07:03", "07/17/2006 12:00", "07/17/2006 12:02",
"07/17/2006 17:00", "07/17/2006 22:00", "07/17/2006 22:01", "07/18/2006 03:01",
"07/18/2006 08:00", "07/18/2006 08:01", "07/18/2006 13:00", "07/18/2006 18:00",
"07/18/2006 23:00", "07/19/2006 04:01", "07/19/2006 09:00", "07/19/2006 09:01",
"07/19/2006 14:00", "07/19/2006 19:00", "07/20/2006 00:00", "07/20/2006 00:01",
"07/20/2006 05:00", "07/20/2006 05:01", "07/20/2006 10:00", "07/20/2006 15:01",
"07/20/2006 20:00", "07/21/2006 01:00", "07/21/2006 06:00", "07/21/2006 11:00",
"07/21/2006 11:01", "07/21/2006 16:00", "07/21/2006 16:01", "07/21/2006 21:00",
"07/22/2006 02:00", "07/22/2006 07:01", "07/22/2006 12:00", "07/22/2006 17:00",
"07/22/2006 17:01", "07/22/2006 22:00", "07/23/2006 03:00", "07/23/2006 08:00",
"07/23/2006 13:00", "07/23/2006 18:00", "07/23/2006 23:00", "07/24/2006 04:00",
"07/24/2006 04:01", "07/24/2006 09:01", "07/24/2006 14:00", "07/24/2006 19:01",
"07/25/2006 00:00", "07/25/2006 00:01", "07/25/2006 05:00", "07/25/2006 05:01",
"07/25/2006 10:01", "07/25/2006 15:00", "07/25/2006 15:01", "07/25/2006 20:01",
"07/26/2006 01:00", "07/26/2006 01:01", "07/26/2006 06:00", "07/26/2006 11:00",
"07/26/2006 16:01", "07/26/2006 21:00", "07/26/2006 21:01", "07/27/2006 02:00",
"07/27/2006 02:01", "07/27/2006 07:00", "07/27/2006 07:01", "07/27/2006 12:00",
"07/27/2006 17:00", "07/27/2006 22:01", "07/28/2006 03:00", "07/28/2006 03:01",
"07/28/2006 08:00", "07/28/2006 08:01", "07/28/2006 13:00", "07/28/2006 18:00",
"07/28/2006 23:01", "07/29/2006 04:00", "07/29/2006 04:03", "07/29/2006 09:00",
"07/29/2006 14:00", "07/29/2006 19:00", "07/30/2006 00:00", "07/30/2006 00:01",
"07/30/2006 05:00", "07/30/2006 05:01", "07/30/2006 10:00", "07/30/2006 10:01",
"07/30/2006 15:00", "07/30/2006 20:01", "07/31/2006 01:01", "07/31/2006 01:03",
"07/31/2006 06:00", "07/31/2006 06:01", "07/31/2006 11:00", "07/31/2006 11:01",
"07/31/2006 16:00", "07/31/2006 21:01"), class = "factor")), .Names = c("ID",
"Long", "Latitude", "DT"), class = "data.frame", row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 706L, 707L, 708L, 709L,
710L, 711L, 712L, 713L, 714L, 715L, 716L))

最佳答案

library(zoo) 可以处理不规则的时间序列,正如 Gabor 所说,Zoo FAQ 有每天聚合多个观察的方法:

z <- zoo( x=data.frame(....), order.by=loc$DateTime ) # translate 'loc' into zoo somehow 

za <- aggregate(z, identity, mean) # take the mean of identically timed objects

...并用局部拟合数据填充缺失数据:
g  <- seq(start(z),end(z),by=1/frequency(z)) 
zi <- na.locf(z,xout=g)

关于r - 不规则时间序列数据 - 我可以让它规则吗?在 r,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6448068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com