irregular time series data- can I make it regular? in r
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'
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?
Here is a sample of what my data looks like:
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", 开发者_如何学JAVA"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) 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)
精彩评论