本文介绍了不规则时间序列数据 - 我可以让它规则吗?在 r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have data that was programmed to acquire information every 5 hours which means multiple data points per day. The problem is sometimes the data logger fails or batteries die or whatever and there are missing data or failed attempts.

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

All of my analysis I do is based upon these dates. I need to sort and filter and select everything based on this date/time stamp. As of right now I am going through "long winded" scripts just to select and subset my data. Additionally I have several data loggers (individuals) so I am also batch processing. For instance, I want to subset and use only one data point per day and I

    #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 吗?

So, is there a way to make my data into time series data to make manipulation easier? Ive seen the discussions for Zoo package a xts - but these seem to require the data to be in regular time series. Ive even had to not use certain scripts because they require the data to be in ts format and I can't figure out how to get my data into that nice formatting. Is there a good resource to teach myself this? Should I even be using ts?

以下是我的数据示例:结构(列表(ID =结构(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), 纬度 = 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 = 结构(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"), 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 有每天聚合多个观察的方法:

library(zoo) can deal with irregular time series, and, as Gabor said, the Zoo FAQ has methods for aggregating multiple observations per day:

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 

...并用局部拟合数据填充缺失数据:

...and fill in missing data with locally fit data:

g  <- seq(start(z),end(z),by=1/frequency(z)) 
zi <- na.locf(z,xout=g)

这篇关于不规则时间序列数据 - 我可以让它规则吗?在 r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:24