Create 5 minute intervals starting from 00 in R - r

I want to divide data in 5 minute intervals and compare them with monthly averages and standard deviation to detect any anomalies. I cut the data in 5 minute intervals as follows:
cut(month.data, breaks = "5 min")
dput(monthlydata)
structure(list(time = c("00:00:00", "00:05:00", "00:10:00", "00:15:00", "00:20:00", "00:25:00", "00:30:00", "00:35:00", "00:40:00", "00:45:00", "00:50:00", "00:55:00", "01:00:00", "01:05:00", "01:10:00", "01:15:00", "01:25:00", "01:30:00", "01:35:00", "01:40:00", "01:45:00", "01:55:00", "02:00:00", "02:20:00", "02:25:00", "02:40:00", "02:45:00", "02:50:00", "03:05:00", "03:15:00", "03:25:00", "03:30:00", "03:35:00", "03:40:00", "03:45:00", "03:50:00", "03:55:00", "04:05:00", "04:25:00", "04:30:00", "04:50:00", "04:55:00", "05:05:00", "05:10:00", "05:15:00", "05:20:00", "05:30:00", "05:40:00", "05:50:00", "05:55:00", "06:00:00", "06:05:00", "06:10:00", "06:15:00", "06:20:00", "06:30:00", "06:35:00", "06:40:00", "06:45:00", "06:55:00", "07:00:00", "07:05:00", "07:10:00", "07:15:00", "07:20:00", "07:25:00", "07:30:00", "07:35:00", "07:40:00", "07:45:00", "07:50:00", "07:55:00", "08:00:00", "08:05:00", "08:10:00", "08:15:00", "08:20:00", "08:25:00", "08:30:00", "08:35:00", "08:40:00", "08:45:00", "08:50:00", "08:55:00", "09:00:00", "09:05:00", "09:10:00", "09:15:00", "09:20:00", "09:25:00", "09:30:00", "09:35:00", "09:40:00", "09:45:00", "09:50:00", "09:55:00", "10:00:00", "10:05:00", "10:10:00", "10:15:00", "10:20:00", "10:25:00", "10:30:00", "10:35:00", "10:40:00", "10:45:00", "10:50:00"), avg = c(50.5510560619622, 43.0189344993435, 50.3126451810161, 51.7984139398903, 44.1216815428764, 54.6443574865021, 50.065771120662, 50.9085361977819, 59.7024337563552, 44.6504863019322, 51.3800349930107, 47.2281110758541, 39.1562855847908, 47.3556824171027, 54.4776585774653, 64.9968044851706, 47.878888210121, 44.7561800618865, 45.0695456307952, 56.1759044802863, 50.8227417957758, 52.6309915011542, 62.7342370217067, 54.4257866432874, 54.3226457929837, 40.1938479668371, 54.5403748037875, 51.8463279336394, 53.8173320832895, 49.9889812414321, 50.5827916556644, 53.5500871960216, 37.0209877205586, 55.2612198888207, 55.1863860227875, 58.2740366768661, 50.5885573635039, 48.1051996319848, 47.7634397043728, 46.5863241465071, 54.1810631439095, 53.7906152156406, 52.6409719258956, 54.81269123308, 44.0537724370726, 47.1977242746078, 46.5010741707819, 45.8433966693518, 42.3623605036368, 43.0730189148746, 58.4167050044254, 49.7799961792657, 53.0755779045083, 52.0552481180891, 42.0602921415756, 52.5126950828788, 55.0870481980705, 44.4144434705709, 42.392966543036, 47.1807241560313, 44.6884956183158, 47.2896481418499, 57.8319708553495, 46.6397655826931, 52.4873442246903, 51.6324293101077, 55.0908694414676, 43.8596455462562, 48.8941181950083, 48.7514252330684, 49.310070422837,48.5234755805063, 58.5542276809981, 47.7720993402378,49.9405735614802, 52.8780543357139, 58.2557463154677,52.9242756783794, 55.4983029733778, 44.2299478555713,57.3379257421419, 50.1951095071188, 53.0553561602009, 50.3109504601222, 44.9316581536335, 47.7568227989573, 50.3093864093436, 50.0461546149579, 48.2373271954793, 50.2943295283144, 46.8054846556807, 48.7084493434669, 46.5421233124519, 50.1222951953386, 49.4207933535255, 46.1521667031027, 51.8644029994928, 53.4831581582472, 51.4972139096679, 51.5181187952616, 49.1998856564675, 50.3454476017966, 49.7587298896826, 45.6040364435812, 49.7466159629413, 51.0983492421099, 52.2935123336372), sd =c(9.32931925004817, 11.6047417906884, 9.80771691435559, 10.2308327194904, 9.67431773674866, 10.5323558825585, 9.1920111408028, 10.0745961985324, 9.21246056157269, 7.96228334027313, 9.89384474113651, 12.3284041772698, 13.5489518864705, 9.96803285037014, 8.85300006821126, 10.9832318078379, 13.0909163134817, 9.7261635496657, 10.3208509302825, 8.74704188744148, 9.44853223258545, 7.68266078719723, 8.78660429415339, 10.1981152232186,10.0617639380203, 9.70826609005244, 10.4405793131911, 10.0294741532956, 10.5843898470973, 10.0678114702352, 9.72005734098214, 8.71988580635692, 13.0339431549482, 8.85263899155544, 11.1140174478773, 10.6801908843647, 11.0442113270832, 9.6576794753704, 11.193361191756, 9.05129333547447, 11.1414887813967, 10.5985803957382, 10.2422686622522, 11.2629702278102, 10.4774845098793, 9.82520095973172, 10.6775347630735, 8.97029695502126, 8.28221177072086, 11.0689605695813, 10.4298020842373, 9.57012379689429, 10.1587613403527, 11.2343452027682, 9.09417849538438, 10.3529463918792, 8.89434012398308, 7.86166740352018, 8.67472741747663, 10.7631616313607, 11.4634738459674, 10.3773119423003, 10.1261492697498, 9.51153382612954, 8.8445878796955, 9.99640290007654, 9.33226675473664, 10.2637002156788, 9.44339377944955, 10.5843418581127, 9.00425609052502, 10.2822169680166, 10.5184763916409, 10.3495044419935, 9.99843947958033, 8.67736455800308, 10.1508472078283, 9.3450979185795, 9.84066593055499, 9.95417999414617, 9.2622985333717, 11.4445218170255, 9.72729168049685, 9.36415135782777, 10.2272372991057, 10.4800042675175, 10.0067598340318, 10.6127856103593, 8.91026514253112, 10.3451668931764, 9.86818949901026, 10.736479643411, 8.6424538599602, 9.53874170759392, 10.5484021386586, 10.7130372286846, 10.2509775336419, 10.7454025452377, 9.63372860813097, 9.35982465121709, 9.47486350661184, 10.311507962123, 10.0338294237329, 9.50474631714252, 10.1746281369343, 9.75040171260143, 10.8675383447689)), class = c("tbl_df", "tbl", "data.frame"), .Names = c("time", "avg", "sd"), row.names = c(NA, -107L))
However if my currdata starts later than 00 min, the time intervals are calculated depending on the earliest entry in the data.So if the first entry is at 01:00 min, the intervals are 01:00 to 06:00 and so on. However, I want the intervals to be 00:00 - 05:00 and so on, to be consistent with monthlydata intervals. Is there a way to set the lowest limit on the intervals in the cut statement? or is there any other way to ensure that both series use the same minutes intervals.
The data for one day
dput(currday)
structure(c(1533769260, 1533769320, 1533769320, 1533769380, 1533769380, 1533769380, 1533769440, 1533769500, 1533769680, 1533769740, 1533769740,1533769740, 1533769800, 1533769920, 1533769920, 1533769920,1533769980,1533770100, 1533770100, 1533770220, 1533770280, 1533770340, 1533770400, 1533770460, 1533770460, 1533770460, 1533770520, 1533770520, 1533770580, 1533770580, 1533770640, 1533770880, 1533771060, 1533771060, 1533771120, 1533771300, 1533771300, 1533771300, 1533771360, 1533771360, 1533771540, 1533771660, 1533771660, 1533771960, 1533772260, 1533772260, 1533772260, 1533772440, 1533772440, 1533772560, 1533772620, 1533772620, 1533772800, 1533772980, 1533773160, 1533773400, 1533773400, 1533773460, 1533773760, 1533773880, 1533774480, 1533774540, 1533774900, 1533774900, 1533774960, 1533775080, 1533775140, 1533775320, 1533775380, 1533775500,1533775560, 1533775680, 1533776340, 1533776520, 1533776520,1533776580, 1533777660, 1533777900, 1533777900, 1533778200, 1533779100, 1533779100, 1533779220, 1533779460, 1533779640, 1533780360, 1533780960, 1533781740, 1533782040, 1533782340, 1533782700, 1533782880, 1533783120, 1533783600, 1533784020, 1533784140, 1533785280, 1533785280, 1533785400, 1533785460, 1533786660), class = c("POSIXct", "POSIXt"), tzone = "Europe/London")
I have looked at other questions and found create-a-24-hour-vector-with-5-minutes-time-interval-in-r. It is not quite what I am after. Any help is highly appreciated.

As suggested by #AleBdC, I checked the link: Grouping every n minutes with dplyr and found the comment suggested by #MikeyHarper very useful.
So, I first create five-minute intervals in currday, then separate the date and time into two columns and then converted the time column into factor and use it for grouping and summarizing.
library(lubridate)
x <- ymd_hms(currday, tz="Europe/London")
x<-data.frame(floor_date(x, unit = "5minutes"))
names(x)<-"by5"
x%>%separate(by5, into = c("date", "time"),sep = "\\s" )
x$time<-as.factor(x$time)
I also created five-minute intervals in my monthly data using the same approach and calculated the mean and sd and compared it with the daily data.

Related

Substracting time in R

I want to calculate the amount of time someone spends in bed at night.
First column, j1bed (time goes to bed):
dput(head(subscales$j1bed, 20))
c("23:00:00", "01:00:00", "22:00:00", "00:00:00", "00:00:00",
"23:00:00", "03:00:00", "23:00:00", "22:00:00", "23:00:00", "21:00:00",
"23:30:00", "22:00:00", "22:00:00", "20:00:00", "22:00:00", "22:00:00",
"21:00:00", "20:00:00", "23:00:00")
Second column, j3wake (time wakes up):
dput(head(subscales$j3wake, 20))
c("08:00:00", "08:00:00", "05:00:00", "08:00:00", "07:00:00",
"07:00:00", "09:00:00", "09:00:00", "08:00:00", "06:00:00", "03:00:00",
"08:30:00", "07:00:00", "08:00:00", "09:00:00", "07:00:00", "06:00:00",
"07:00:00", "07:00:00", "12:00:00")
My code currently is as follows:
Hours_in_Bed<- as.numeric(difftime(strptime(subscales$j3wake, "%I:%M %p" ),strptime(subscales$j1bed, "%I:%M %p" ),units='hours'))
For some reason, this is coming up with only "NA". Any ideas?
Edit: I've tried the following three suggestions:
1)
result1<-strptime(subscales$j3wake, "%I:%M %p")
result2<-strptime(subscales$j1bed, "%I:%M %p")
Hours_in_Bed<-as.numeric(difftime(result2, result1, units='hours'))
This yields only "NA"s.
2)
subscales$j3wake1=as.POSIXct(paste('1970-1-1',subscales$j3wake))
subscales$j1bed1 =as.POSIXct(paste('1970-1-1',subscales$j1bed))
as.numeric(difftime(subscales$j1bed1,subscales$j3wake1,units='hours'))
This yields "0"s.
3)
all(grepl("[0-9]{2}:[0-9]{2} (AM|PM)", subscales$j3wake))
all(grepl("[0-9]{2}:[0-9]{2} (AM|PM)", subscales$j1bed))
Both yield "False".
Edit 2: that last tip was key! I think I should be using "%H:%M" instead:
Hours_in_Bed<- as.numeric(difftime(strptime(subscales$j3wake, "%H:%M" ),strptime(subscales$j1bed, "%H:%M" ),units='hours'))
If I then do:
Hours_in_Bed[!is.na(Hours_in_Bed) & Hours_in_Bed<0] <- 24 + Hours_in_Bed[!is.na(Hours_in_Bed) & Hours_in_Bed<0]
I should be good to go. Let me know if I'm still off, and thanks for the help!
It's usually easier to use the POSIXct-type even if there isn't any real year/month/day. Example:
subscales$j3wake1 = as.POSIXct(paste('1970-1-1',subscales$j3wake))
subscales$j1bed1 = as.POSIXct(paste('1970-1-1',subscales$j1bed))
as.numeric(difftime(subscales$j1bed1,subscales$j3wake1,units='hours'))

Getting date to appear on x axis of plot.stl method in R

I was wondering if anyone knows of a way to get the dates to appear on the x axis of an stl plot.
res<- (stl(ts(data[,variable],frequency=52,start=as.Date(data[1,date]) ),s.window="per",robust=TRUE) )
plot(res)
However this is just producing some unicode labels at the bottom of the chart, I have also tried chaning as.Date to as.character however this didn't work.
dput of some data:
structure(c("2007-01-01", "2007-01-08", "2007-01-15", "2007-01-22",
"2007-01-29", "2007-02-05", "2007-02-12", "2007-02-19", "2007-02-26",
"2007-03-05", "2007-03-12", "2007-03-19", "2007-03-26", "2007-04-02",
"2007-04-09", "2007-04-16", "2007-04-23", "2007-04-30", "2007-05-07",
"2007-05-14", "2007-05-21", "2007-05-28", "2007-06-04", "2007-06-11",
"2007-06-18", "2007-06-25", "2007-07-02", "2007-07-09", "2007-07-16",
"2007-07-23", "2007-07-30", "2007-08-06", "2007-08-13", "2007-08-20",
"2007-08-27", "2007-09-03", "2007-09-10", "2007-09-17", "2007-09-24",
"2007-10-01", "2007-10-08", "2007-10-15", "2007-10-22", "2007-10-29",
"2007-11-05", "2007-11-12", "2007-11-19", "2007-11-26", "2007-12-03",
"2007-12-10", "2007-12-17", "2007-12-24", "2007-12-31", "2008-01-07",
"2008-01-14", "2008-01-21", "2008-01-28", "2008-02-04", "2008-02-11",
"2008-02-18", "2008-02-25", "2008-03-03", "2008-03-10", "2008-03-17",
"2008-03-24", "2008-03-31", "2008-04-07", "2008-04-14", "2008-04-21",
"2008-04-28", "2008-05-05", "2008-05-12", "2008-05-19", "2008-05-26",
"2008-06-02", "2008-06-09", "2008-06-16", "2008-06-23", "2008-06-30",
"2008-07-07", "2008-07-14", "2008-07-21", "2008-07-28", "2008-08-04",
"2008-08-11", "2008-08-18", "2008-08-25", "2008-09-01", "2008-09-08",
"2008-09-15", "2008-09-22", "2008-09-29", "2008-10-06", "2008-10-13",
"2008-10-20", "2008-10-27", "2008-11-03", "2008-11-10", "2008-11-17",
"2008-11-24", "2008-12-01", "2008-12-08", "2008-12-15", "2008-12-22",
"2008-12-29", "2009-01-05", "2009-01-12", "2009-01-19", "2009-01-26",
"2009-02-02", "2009-02-09", "2009-02-16", "2009-02-23", "2009-03-02",
"2009-03-09", "2009-03-16", "2009-03-23", "2009-03-30", "2009-04-06",
"2009-04-13", "2009-04-20", "2009-04-27", "2009-05-04", "2009-05-11",
"2009-05-18", "2009-05-25", "2009-06-01", "2009-06-08", "2009-06-15",
"2009-06-22", "2009-06-29", "2009-07-06", "2009-07-13", "2009-07-20",
"2009-07-27", "2009-08-03", "2009-08-10", "2009-08-17", "2009-08-24",
"2009-08-31", "2009-09-07", "2009-09-14", "2009-09-21", "2009-09-28",
"2009-10-05", "2009-10-12", "2009-10-19", "2009-10-26", "2009-11-02",
"2009-11-09", "2009-11-16", "2009-11-23", "2009-11-30", "2009-12-07",
"2009-12-14", "2009-12-21", "2009-12-28", "2010-01-04", "2010-01-11",
"2010-01-18", "2010-01-25", "2010-02-01", "2010-02-08", "2010-02-15",
"2010-02-22", "2010-03-01", "2010-03-08", "2010-03-15", "2010-03-22",
"2010-03-29", "2010-04-05", "2010-04-12", "2010-04-19", "2010-04-26",
"2010-05-03", "2010-05-10", "2010-05-17", "2010-05-24", "2010-05-31",
"2010-06-07", "2010-06-14", "2010-06-21", "2010-06-28", "2010-07-05",
"2010-07-12", "2010-07-19", "2010-07-26", "2010-08-02", "2010-08-09",
"2010-08-16", "2010-08-23", "2010-08-30", "2010-09-06", "2010-09-13",
"2010-09-20", "2010-09-27", "2010-10-04", "2010-10-11", "2010-10-18",
"2010-10-25", "2010-11-01", "2010-11-08", "2010-11-15", "2010-11-22",
"2010-11-29", "2010-12-06", "2010-12-13", "2010-12-20", "2010-12-27",
"2011-01-03", "2011-01-10", "2011-01-17", "2011-01-24", "2011-01-31",
"2011-02-07", "2011-02-14", "2011-02-21", "2011-02-28", "2011-03-07",
"2011-03-14", "2011-03-21", "2011-03-28", "2011-04-04", "2011-04-11",
"2011-04-18", "2011-04-25", "2011-05-02", "2011-05-09", "2011-05-16",
"2011-05-23", "2011-05-30", "2011-06-06", "2011-06-13", "2011-06-20",
"2011-06-27", "2011-07-04", "2011-07-11", "2011-07-18", "2011-07-25",
"2011-08-01", "2011-08-08", "2011-08-15", "2011-08-22", "2011-08-29",
"2011-09-05", "2011-09-12", "2011-09-19", "2011-09-26", "2011-10-03",
"2011-10-10", "2011-10-17", "2011-10-24", "2011-10-31", "2011-11-07",
"2011-11-14", "2011-11-21", "2011-11-28", "2011-12-05", "2011-12-12",
"2011-12-19", "2011-12-26", "442573", "452832", "452785", "459228",
"479509", "477631", "465619", "462001", "485567", "462381", "456059",
"457401", "474094", "468766", "456945", "539126", "545640", "511801",
"486619", "484430", "481428", "470622", "479677", "486755", "477617",
"483656", "497479", "493436", "480080", "481527", "516029", "532349",
"503939", "472171", "461550", "504532", "489715", "480032", "488376",
"470772", "467395", "488155", "455712", "474456", "471237", "482943",
"459320", "456956", "465056", "461930", "441201", "451255", "464508",
"449199", "455411", "476323", "521761", "513416", "521070", "497596",
"485461", "485593", "461148", "429938", "441207", "459484", "462099",
"469285", "454395", "456729", "469251", "517727", "526719", "477985",
"484538", "469766", "472399", "481162", "479039", "481071", "485068",
"462108", "459079", "452410", "488574", "502166", "504323", "514529",
"524206", "504315", "462935", "461899", "461551", "455491", "456857",
"443314", "454586", "458943", "450555", "454311", "442808", "440126",
"414876", "415787", "413352", "423864", "448319", "415440", "431948",
"433313", "448509", "436400", "454154", "454183", "447735", "452220",
"451433", "472808", "446767", "426595", "463693", "435673", "452704",
"456828", "465069", "448685", "457353", "443859", "463972", "480139",
"493342", "500982", "529602", "527365", "512005", "484585", "455007",
"470006", "477522", "443956", "459038", "488877", "466669", "476242",
"470862", "457298", "466438", "454062", "460216", "466645", "445113",
"457255", "451553", "451504", "447991", "435100", "416140", "481390",
"489041", "496984", "486245", "478191", "455201", "448085", "459097",
"480859", "490248", "462523", "489755", "468391", "460229", "481276",
"472845", "481099", "476435", "487314", "475043", "476847", "461928",
"483488", "479379", "456732", "461538", "480773", "471101", "459898",
"482129", "464356", "459420", "457850", "456764", "438152", "467928",
"464732", "458767", "470256", "449488", "433263", "428548", "435099",
"429163", "424251", "434723", "425841", "399498", "418791", "405051",
"385037", "425144", "418296", "397644", "414283", "431907", "429117",
"424862", "439664", "432791", "443588", "434985", "442418", "445760",
"449290", "451412", "456247", "444372", "441390", "458192", "456435",
"450670", "447609", "439083", "464513", "462784", "439423", "450857",
"442374", "447753", "440207", "435254", "430841", "437233", "426523",
"430127", "431305", "470244", "508878", "511064", "504182", "462076",
"452218", "426535", "436892", "459008", "441449", "438783", "427497",
"432275", "436745", "423068", "429574", "416074"), .Dim = c(261L,
2L))
The problem is with how you have created the time series object.
?ts will give you
start : the time of the first observation. Either a single number or a
vector of two integers, which specify a natural time unit and a
(1-based) number of samples into the time unit.
Since your data starts at 2007-01-01 and is a weekly data, you have to specify c(2007,1), Note that the "1" here denotes that data start from the 1st week of 2007, as the data is weekly. Here the frequency should be set to 52 because, after 52 cycles/weeks a year will change from 2007 to 2008.
This should work
res<- (stl(ts(data[,2],frequency=52,start=c(2007,1 )),s.window="per",robust=TRUE) )

R - Color or shade area between lines

I'm trying to replicate with R a chart I made on Excel, which should represent a 95% Confidence Interval (CI) around a time series forecast. The Excel chart looks like this:
So, basically, the original historical time series and from a certain point in time the forecast of what it could be with its respective CI.
They way it's done on Excel is a bit inefficient:
I have four time series which overlap much of the time;
The actual/historical time series (blue line above) simply stops when the forecast begins;
The forecast (dotted red above) is simply hidden below the blue one until the forecast period begins;
Then I have a time series representing the difference between the the upper bound and the lower bound of the CI, which playing around with Excel Stacked Areas charts, becomes the shaded area in the chart above.
Obviously, the computation to generate the forecast and the CIs is much faster and easier to generalize and use with R, and while I could complete the task on R and then simply copy the output on Excel to draw the chart, doing everything in R would be much nicer.
At the end of the question I provided the raw data with dput() as suggested by #MLavoie.
Here the packages I loaded (not sure you need them all here, but they are the ones I usually work with):
require(zoo)
require(xts)
require(lattice)
require(latticeExtra)
My data looks like this for the first 100 rows:
> head(data)
fifth_percentile Median nintyfifth_percentile
2017-06-18 1.146267 1.146267 1.146267
2017-06-19 1.134643 1.134643 1.134643
2017-06-20 1.125664 1.125664 1.125664
2017-06-21 1.129037 1.129037 1.129037
2017-06-22 1.147542 1.147542 1.147542
2017-06-23 1.159989 1.159989 1.159989
Then after the 100 data point, the time series start to diverge and at the end they look like this:
> tail(data)
fifth_percentile Median nintyfifth_percentile
2017-12-30 0.9430930 1.125844 1.341603
2017-12-31 0.9435227 1.127391 1.354928
2018-01-01 0.9417235 1.124625 1.355527
2018-01-02 0.9470077 1.124088 1.361420
2018-01-03 0.9571596 1.127299 1.364005
2018-01-04 0.9515535 1.127978 1.369536
Solution provided by DaveTurek
Thanks to DaveTurek I've found the answer. However, only difference is that for my xts dataframe, apparently, I need first to convert each column to numbers (with as.numeric()). No idea if that stems from me doing something wrong with xts and lattice, or it is the only way to achieve it using DaveTurek suggestion. Will try to investigate it further.
Here is the code to generate the chart:
x = index(data[1:100,2])
y = as.numeric(data[1:100,2])
ex.x = index(data[101:200,2])
ex.y = as.numeric(data[101:200,2])
ex.lo = as.numeric(data[101:200,1])
ex.hi = as.numeric(data[101:200,3])
xyplot(y~x, ylim = c(0.9,1.4),
panel=function(x,y,...) {
panel.lines(x,y,lwd=2,col=4)
panel.polygon(c(ex.x,rev(ex.x)),c(ex.lo,rev(ex.hi)),border=NA,col=5)
panel.lines(ex.x,ex.y,lwd=2,col=2)
})
And here the final result:
Here is the final dataset, from dput(), that I'm trying to plot:
> dput(data)
structure(c(1.14626724930899, 1.13464279067717, 1.12566420479952,
1.12903662366847, 1.14754211999921, 1.15998855701439, 1.15274364578958,
1.16226441955745, 1.16169992687419, 1.16520028734587, 1.16823402018407,
1.19832130049664, 1.18411773220697, 1.18531274215286, 1.16421444455115,
1.17108139956539, 1.18392357740377, 1.20103911352579, 1.17791736605905,
1.18277944964829, 1.20162550199013, 1.19665058179752, 1.19411188122108,
1.19367558590966, 1.19803272562951, 1.20600155861871, 1.22189449901607,
1.22072774140118, 1.22312376195254, 1.25355505518571, 1.25895911759195,
1.2613354420716, 1.24440525381363, 1.24444079462029, 1.24168652168112,
1.24154936710117, 1.23440527301777, 1.22592718438811, 1.21709102449773,
1.21448030929365, 1.23109601090898, 1.24401127451953, 1.23953314346685,
1.21863565024168, 1.20834325548551, 1.20281193695583, 1.20405850724191,
1.19608032796923, 1.22008184095742, 1.21675995421116, 1.20198916403093,
1.20029121301547, 1.18822375424598, 1.19007923345344, 1.19285965857709,
1.1971013197471, 1.1776860331227, 1.18028531916998, 1.18394951589397,
1.16712430930941, 1.17827461393349, 1.18751430033172, 1.21482260909863,
1.2167262724184, 1.21729489152574, 1.21847062594996, 1.21932070698031,
1.19678189566773, 1.17678214957629, 1.17586968485613, 1.16903708967946,
1.16967697995898, 1.14498266161799, 1.12782282645368, 1.11540004479973,
1.12639853863918, 1.11402516325222, 1.10511837662567, 1.10600107687395,
1.10243149863659, 1.10404564773364, 1.12949458422398, 1.11679224666313,
1.11338078540871, 1.10762728498848, 1.12437898939299, 1.11572706259347,
1.1148111967932, 1.12358625045939, 1.11169207274881, 1.13009253108247,
1.13772927166761, 1.12550770863279, 1.13062401691547, 1.12821231512428,
1.13174620070443, 1.13072790983063, 1.1428325334377, 1.12739171867048,
1.1214997813059, 1.11870510839984, 1.096148222775, 1.08805136310032,
1.08701594286129, 1.08047984136855, 1.07939438148434, 1.0684082570972,
1.06497159411023, 1.05820047926833, 1.06322519359802, 1.06234781015662,
1.05431808916504, 1.054405104791, 1.05330182895869, 1.04787681441803,
1.041698698458, 1.03870702538097, 1.03300007904201, 1.02741553353049,
1.03525701392318, 1.0339774223954, 1.0328464056954, 1.03100871401712,
1.03348765946373, 1.03473218333386, 1.02942612874379, 1.02109481188296,
1.02301597272716, 1.01553904377803, 1.0031650628692, 1.00779708136199,
1.01322764666693, 1.01964272925677, 1.02125480865504, 1.02300342204156,
1.02563993245866, 1.02972111884963, 1.02048756192688, 1.00481457379443,
1.00512607721887, 1.01094340128446, 1.01377432300649, 1.01170553705668,
1.00551128145228, 1.00612634442438, 1.00735643866839, 1.0080606590012,
0.985706701720841, 0.982234200010558, 0.975314534071082, 0.973611418201841,
0.968118612511537, 0.973092829667201, 0.975599110408158, 0.967214930243667,
0.968569928969912, 0.963572085616274, 0.964901787179726, 0.957782708788541,
0.951868416101986, 0.956694066411684, 0.956937537219092, 0.956303331651844,
0.947880835881923, 0.956308493824626, 0.948146077843001, 0.945939091828748,
0.945082701640947, 0.937222489932819, 0.937989843132858, 0.948712728941467,
0.939050882255992, 0.946264846068344, 0.944926693194716, 0.946825914432391,
0.939070104432721, 0.950666108330947, 0.949365988007735, 0.943616625744159,
0.946600795357699, 0.941276090147603, 0.939957902451166, 0.941523527816784,
0.946611480333791, 0.959236316317354, 0.96165367272139, 0.957508302724503,
0.954774123925477, 0.960811125123549, 0.956525507301749, 0.948237690612711,
0.951299123137395, 0.945212566792479, 0.94507842203255, 0.942735006048921,
0.943093032220433, 0.943522672031737, 0.941723495992432, 0.947007713852018,
0.95715960245335, 0.951553478810637, 1.14626724930899, 1.13464279067717,
1.12566420479952, 1.12903662366847, 1.14754211999921, 1.15998855701439,
1.15274364578958, 1.16226441955745, 1.16169992687419, 1.16520028734587,
1.16823402018407, 1.19832130049664, 1.18411773220697, 1.18531274215286,
1.16421444455115, 1.17108139956539, 1.18392357740377, 1.20103911352579,
1.17791736605905, 1.18277944964829, 1.20162550199013, 1.19665058179752,
1.19411188122108, 1.19367558590966, 1.19803272562951, 1.20600155861871,
1.22189449901607, 1.22072774140118, 1.22312376195254, 1.25355505518571,
1.25895911759195, 1.2613354420716, 1.24440525381363, 1.24444079462029,
1.24168652168112, 1.24154936710117, 1.23440527301777, 1.22592718438811,
1.21709102449773, 1.21448030929365, 1.23109601090898, 1.24401127451953,
1.23953314346685, 1.21863565024168, 1.20834325548551, 1.20281193695583,
1.20405850724191, 1.19608032796923, 1.22008184095742, 1.21675995421116,
1.20198916403093, 1.20029121301547, 1.18822375424598, 1.19007923345344,
1.19285965857709, 1.1971013197471, 1.1776860331227, 1.18028531916998,
1.18394951589397, 1.16712430930941, 1.17827461393349, 1.18751430033172,
1.21482260909863, 1.2167262724184, 1.21729489152574, 1.21847062594996,
1.21932070698031, 1.19678189566773, 1.17678214957629, 1.17586968485613,
1.16903708967946, 1.16967697995898, 1.14498266161799, 1.12782282645368,
1.11540004479973, 1.12639853863918, 1.11402516325222, 1.10511837662567,
1.10600107687395, 1.10243149863659, 1.10404564773364, 1.12949458422398,
1.11679224666313, 1.11338078540871, 1.10762728498848, 1.12437898939299,
1.11572706259347, 1.1148111967932, 1.12358625045939, 1.11169207274881,
1.13009253108247, 1.13772927166761, 1.12550770863279, 1.13062401691547,
1.12821231512428, 1.13174620070443, 1.13072790983063, 1.1428325334377,
1.12739171867048, 1.1214997813059, 1.11870510839984, 1.11811303551412,
1.11855383782522, 1.11981261957516, 1.12096887905804, 1.12162710713999,
1.12015553029278, 1.12189306008921, 1.1236834173899, 1.12204149206779,
1.12075809542535, 1.12116672935174, 1.12216772364685, 1.11821915571021,
1.12117719223463, 1.11896003906963, 1.11563621625852, 1.1183625095638,
1.12053072892388, 1.1216348268255, 1.12317377733957, 1.11873136428952,
1.12267083202989, 1.12642930089215, 1.13027646770951, 1.13129632891931,
1.12700346009603, 1.12060488827701, 1.12390899402613, 1.13129350591169,
1.12786650327192, 1.1274201121913, 1.13101906643359, 1.12727135093377,
1.12458327192256, 1.12259738972645, 1.12097982776572, 1.12073621452193,
1.12364872830763, 1.12644326299714, 1.12556263098661, 1.12797963752343,
1.12734519199847, 1.1261793072762, 1.12911407446825, 1.12754878937943,
1.12777579027467, 1.12554965831588, 1.12324469267853, 1.12231558194992,
1.12135908710208, 1.11923353817423, 1.12345300992675, 1.12186883237389,
1.12173652640663, 1.12488148969114, 1.12664301925369, 1.12294230775256,
1.12393650688095, 1.13038044949978, 1.12822226676967, 1.12934384230215,
1.1217648908055, 1.12218158739803, 1.12302651609468, 1.12682187689922,
1.13537701046932, 1.13172108462183, 1.1374053505525, 1.13498257452656,
1.12692005654471, 1.13210629725645, 1.12868775509168, 1.13073909215368,
1.13098804355869, 1.13353301668386, 1.13336476594698, 1.13233873705211,
1.12667020676157, 1.12133152301322, 1.12418759586717, 1.12048022460741,
1.12798162212357, 1.13053093896994, 1.12019367019997, 1.12422483586498,
1.11303086301782, 1.11986711815552, 1.12504718249418, 1.11341517044014,
1.12495096618792, 1.12995127061511, 1.13538401552385, 1.13145536081928,
1.1264465959783, 1.12584386458867, 1.1273908895838, 1.12462482614994,
1.1240880626286, 1.12729907535003, 1.12797751377714, 1.14626724930899,
1.13464279067717, 1.12566420479952, 1.12903662366847, 1.14754211999921,
1.15998855701439, 1.15274364578958, 1.16226441955745, 1.16169992687419,
1.16520028734587, 1.16823402018407, 1.19832130049664, 1.18411773220697,
1.18531274215286, 1.16421444455115, 1.17108139956539, 1.18392357740377,
1.20103911352579, 1.17791736605905, 1.18277944964829, 1.20162550199013,
1.19665058179752, 1.19411188122108, 1.19367558590966, 1.19803272562951,
1.20600155861871, 1.22189449901607, 1.22072774140118, 1.22312376195254,
1.25355505518571, 1.25895911759195, 1.2613354420716, 1.24440525381363,
1.24444079462029, 1.24168652168112, 1.24154936710117, 1.23440527301777,
1.22592718438811, 1.21709102449773, 1.21448030929365, 1.23109601090898,
1.24401127451953, 1.23953314346685, 1.21863565024168, 1.20834325548551,
1.20281193695583, 1.20405850724191, 1.19608032796923, 1.22008184095742,
1.21675995421116, 1.20198916403093, 1.20029121301547, 1.18822375424598,
1.19007923345344, 1.19285965857709, 1.1971013197471, 1.1776860331227,
1.18028531916998, 1.18394951589397, 1.16712430930941, 1.17827461393349,
1.18751430033172, 1.21482260909863, 1.2167262724184, 1.21729489152574,
1.21847062594996, 1.21932070698031, 1.19678189566773, 1.17678214957629,
1.17586968485613, 1.16903708967946, 1.16967697995898, 1.14498266161799,
1.12782282645368, 1.11540004479973, 1.12639853863918, 1.11402516325222,
1.10511837662567, 1.10600107687395, 1.10243149863659, 1.10404564773364,
1.12949458422398, 1.11679224666313, 1.11338078540871, 1.10762728498848,
1.12437898939299, 1.11572706259347, 1.1148111967932, 1.12358625045939,
1.11169207274881, 1.13009253108247, 1.13772927166761, 1.12550770863279,
1.13062401691547, 1.12821231512428, 1.13174620070443, 1.13072790983063,
1.1428325334377, 1.12739171867048, 1.1214997813059, 1.11870510839984,
1.14162401974592, 1.15630966411729, 1.15992199767135, 1.16683144867851,
1.16928280999155, 1.17287782220285, 1.18184525262982, 1.17555305757354,
1.18031492211593, 1.18142628277888, 1.18307577052783, 1.18257404220722,
1.19421117710041, 1.19403330560815, 1.19510080390052, 1.2058940348108,
1.19848571699109, 1.20138771250604, 1.20660682710938, 1.20790011589089,
1.20963951875753, 1.21572259411602, 1.21379678812156, 1.220302087399,
1.22062959185172, 1.22743877731977, 1.23135277550334, 1.24075667733246,
1.24169498945046, 1.23529301399753, 1.2399941777708, 1.24823732280171,
1.23861121958778, 1.24816319854615, 1.25252933549084, 1.25133386983018,
1.24512546001264, 1.2617641352045, 1.25486018976211, 1.25424601859098,
1.25820538036104, 1.25968528498312, 1.26939611029084, 1.27883933177157,
1.27926882841012, 1.27951234203094, 1.28997494816278, 1.29391898267335,
1.2971442938215, 1.29733541086814, 1.30376525837809, 1.31025722802128,
1.29718190520268, 1.27919305871102, 1.28685138548374, 1.28594279969497,
1.28695233433419, 1.30277136510213, 1.29178316107299, 1.29586799884087,
1.30076586308517, 1.30881154838964, 1.32171887794143, 1.3197588324899,
1.3121332301804, 1.31744410759858, 1.31402945919721, 1.30926303329755,
1.32019231597949, 1.31449633135152, 1.31730801686101, 1.31834557852015,
1.3175761022299, 1.33430488507454, 1.34091614601639, 1.33606628597812,
1.33180446732765, 1.33630738683041, 1.33449101077219, 1.32521028784732,
1.32241490851887, 1.31488015995544, 1.31913131799656, 1.32901121011698,
1.33177659436063, 1.32577077582349, 1.31960627618725, 1.31307169067904,
1.32148403094167, 1.33104893196281, 1.33491831741272, 1.3386091981919,
1.35730874062825, 1.3460340606746, 1.34160318929376, 1.35492848895938,
1.35552729646417, 1.36141957863605, 1.36400538435282, 1.369536167295),
.indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
class = c("xts", "zoo"), index = structure(c(1497744000, 1497830400, 1497916800,
1498003200, 1498089600, 1498176000, 1498262400, 1498348800, 1498435200,
1498521600, 1498608000, 1498694400, 1498780800, 1498867200, 1498953600,
1499040000, 1499126400, 1499212800, 1499299200, 1499385600, 1499472000,
1499558400, 1499644800, 1499731200, 1499817600, 1499904000, 1499990400,
1500076800, 1500163200, 1500249600, 1500336000, 1500422400, 1500508800,
1500595200, 1500681600, 1500768000, 1500854400, 1500940800, 1501027200,
1501113600, 1501200000, 1501286400, 1501372800, 1501459200, 1501545600,
1501632000, 1501718400, 1501804800, 1501891200, 1501977600, 1502064000,
1502150400, 1502236800, 1502323200, 1502409600, 1502496000, 1502582400,
1502668800, 1502755200, 1502841600, 1502928000, 1503014400, 1503100800,
1503187200, 1503273600, 1503360000, 1503446400, 1503532800, 1503619200,
1503705600, 1503792000, 1503878400, 1503964800, 1504051200, 1504137600,
1504224000, 1504310400, 1504396800, 1504483200, 1504569600, 1504656000,
1504742400, 1504828800, 1504915200, 1505001600, 1505088000, 1505174400,
1505260800, 1505347200, 1505433600, 1505520000, 1505606400, 1505692800,
1505779200, 1505865600, 1505952000, 1506038400, 1506124800, 1506211200,
1506297600, 1506384000, 1506470400, 1506556800, 1506643200, 1506729600,
1506816000, 1506902400, 1506988800, 1507075200, 1507161600, 1507248000,
1507334400, 1507420800, 1507507200, 1507593600, 1507680000, 1507766400,
1507852800, 1507939200, 1508025600, 1508112000, 1508198400, 1508284800,
1508371200, 1508457600, 1508544000, 1508630400, 1508716800, 1508803200,
1508889600, 1508976000, 1509062400, 1509148800, 1509235200, 1509321600,
1509408000, 1509494400, 1509580800, 1509667200, 1509753600, 1509840000,
1509926400, 1510012800, 1510099200, 1510185600, 1510272000, 1510358400,
1510444800, 1510531200, 1510617600, 1510704000, 1510790400, 1510876800,
1510963200, 1511049600, 1511136000, 1511222400, 1511308800, 1511395200,
1511481600, 1511568000, 1511654400, 1511740800, 1511827200, 1511913600,
1.512e+09, 1512086400, 1512172800, 1512259200, 1512345600, 1512432000,
1512518400, 1512604800, 1512691200, 1512777600, 1512864000, 1512950400,
1513036800, 1513123200, 1513209600, 1513296000, 1513382400, 1513468800,
1513555200, 1513641600, 1513728000, 1513814400, 1513900800, 1513987200,
1514073600, 1514160000, 1514246400, 1514332800, 1514419200, 1514505600,
1514592000, 1514678400, 1514764800, 1514851200, 1514937600, 1515024000
), tzone = "UTC", tclass = "Date"), .Dim = c(201L, 3L), .Dimnames = list(
NULL, c("fifth_percentile", "Median", "nintyfifth_percentile"
)))
I haven't tried with your data, but if the question is how to shade the forecast area, maybe this simple example will help.
library(lattice)
x = 1:12 # base data
y = x
ex.x = 12:16 # extrapolated data
ex.y = 12:16
ex.lo = 12+0:4*.3 # lower bound
ex.hi = 12+0:4*1.6 # upper bound
xyplot(y~x,xlim=c(0:18),ylim=c(0:20),
panel=function(x,y,...) {
panel.lines(x,y,lwd=2,col=4)
panel.polygon(c(ex.x,rev(ex.x)),c(ex.lo,rev(ex.hi)),border=NA,col=5)
panel.lines(ex.x,ex.y,lwd=2,col=2)
})
You can add the shaded polygon to the lattice plot in a panel function. I used c(ex.x,rev(ex.x)) and c(ex.lo,rev(ex.hi)) to construct the polygon boundary.

strptime() function in R converting time values to 0

I have a column in a data frame (factor type) that I want to convert to POSIXlt using strptime(). What I'm doing is:
file[, 2] <- strptime(file[, 2], format="%H:%M:%S")
Examples of values in the column are: 17:24:00 17:25:00 17:26:00
However, the function returns all 0s. Is the format specified incorrectly? Any help would be appreciated.
Here's the output of dput(head(file[,2])):
structure(1045:1050, .Label = c("00:00:00", "00:01:00", "00:02:00",
"00:03:00", "00:04:00", "00:05:00", "00:06:00", "00:07:00", "00:08:00",
"00:09:00", "00:10:00", "00:11:00", "00:12:00", "00:13:00", "00:14:00",
"00:15:00", "00:16:00", "00:17:00", "00:18:00", "00:19:00", "00:20:00",
"00:21:00", "00:22:00", "00:23:00", "00:24:00", "00:25:00", "00:26:00",
"00:27:00", "00:28:00", "00:29:00", "00:30:00", "00:31:00", "00:32:00",
"00:33:00", "00:34:00", "00:35:00", "00:36:00", "00:37:00", "00:38:00",
"00:39:00", "00:40:00", "00:41:00", "00:42:00", "00:43:00", "00:44:00",
"00:45:00", "00:46:00", "00:47:00", "00:48:00", "00:49:00", "00:50:00",
"00:51:00", "00:52:00", "00:53:00", "00:54:00", "00:55:00", "00:56:00",
"00:57:00", "00:58:00", "00:59:00", "01:00:00", "01:01:00", "01:02:00",
"01:03:00", "01:04:00", "01:05:00", "01:06:00", "01:07:00", "01:08:00",
"01:09:00", "01:10:00", "01:11:00", "01:12:00", "01:13:00", "01:14:00",
"01:15:00", "01:16:00", "01:17:00", "01:18:00", "01:19:00", "01:20:00",
"01:21:00", "01:22:00", "01:23:00", "01:24:00", "01:25:00", "01:26:00",
"01:27:00", "01:28:00", "01:29:00", "01:30:00", "01:31:00", "01:32:00",
"01:33:00", "01:34:00", "01:35:00", "01:36:00", "01:37:00", "01:38:00",
"01:39:00", "01:40:00", "01:41:00", "01:42:00", "01:43:00", "01:44:00",
"01:45:00", "01:46:00", "01:47:00", "01:48:00", "01:49:00", "01:50:00",
"01:51:00", "01:52:00", "01:53:00", "01:54:00", "01:55:00", "01:56:00",
"01:57:00", "01:58:00", "01:59:00", "02:00:00", "02:01:00", "02:02:00",
"02:03:00", "02:04:00", "02:05:00", "02:06:00", "02:07:00", "02:08:00",
"02:09:00", "02:10:00", "02:11:00", "02:12:00", "02:13:00", "02:14:00",
"02:15:00", "02:16:00", "02:17:00", "02:18:00", "02:19:00", "02:20:00",
"02:21:00", "02:22:00", "02:23:00", "02:24:00", "02:25:00", "02:26:00",
"02:27:00", "02:28:00", "02:29:00", "02:30:00", "02:31:00", "02:32:00",
"02:33:00", "02:34:00", "02:35:00", "02:36:00", "02:37:00", "02:38:00",
"02:39:00", "02:40:00", "02:41:00", "02:42:00", "02:43:00", "02:44:00",
"02:45:00", "02:46:00", "02:47:00", "02:48:00", "02:49:00", "02:50:00",
"02:51:00", "02:52:00", "02:53:00", "02:54:00", "02:55:00", "02:56:00",
"02:57:00", "02:58:00", "02:59:00", "03:00:00", "03:01:00", "03:02:00",
"03:03:00", "03:04:00", "03:05:00", "03:06:00", "03:07:00", "03:08:00",
"03:09:00", "03:10:00", "03:11:00", "03:12:00", "03:13:00", "03:14:00",
"03:15:00", "03:16:00", "03:17:00", "03:18:00", "03:19:00", "03:20:00",
"03:21:00", "03:22:00", "03:23:00", "03:24:00", "03:25:00", "03:26:00",
"03:27:00", "03:28:00", "03:29:00", "03:30:00", "03:31:00", "03:32:00",
"03:33:00", "03:34:00", "03:35:00", "03:36:00", "03:37:00", "03:38:00",
"03:39:00", "03:40:00", "03:41:00", "03:42:00", "03:43:00", "03:44:00",
"03:45:00", "03:46:00", "03:47:00", "03:48:00", "03:49:00", "03:50:00",
"03:51:00", "03:52:00", "03:53:00", "03:54:00", "03:55:00", "03:56:00",
"03:57:00", "03:58:00", "03:59:00", "04:00:00", "04:01:00", "04:02:00",
"04:03:00", "04:04:00", "04:05:00", "04:06:00", "04:07:00", "04:08:00",
"04:09:00", "04:10:00", "04:11:00", "04:12:00", "04:13:00", "04:14:00",
"04:15:00", "04:16:00", "04:17:00", "04:18:00", "04:19:00", "04:20:00",
"04:21:00", "04:22:00", "04:23:00", "04:24:00", "04:25:00", "04:26:00",
"04:27:00", "04:28:00", "04:29:00", "04:30:00", "04:31:00", "04:32:00",
"04:33:00", "04:34:00", "04:35:00", "04:36:00", "04:37:00", "04:38:00",
"04:39:00", "04:40:00", "04:41:00", "04:42:00", "04:43:00", "04:44:00",
"04:45:00", "04:46:00", "04:47:00", "04:48:00", "04:49:00", "04:50:00",
"04:51:00", "04:52:00", "04:53:00", "04:54:00", "04:55:00", "04:56:00",
"04:57:00", "04:58:00", "04:59:00", "05:00:00", "05:01:00", "05:02:00",
"05:03:00", "05:04:00", "05:05:00", "05:06:00", "05:07:00", "05:08:00",
"05:09:00", "05:10:00", "05:11:00", "05:12:00", "05:13:00", "05:14:00",
"05:15:00", "05:16:00", "05:17:00", "05:18:00", "05:19:00", "05:20:00",
"05:21:00", "05:22:00", "05:23:00", "05:24:00", "05:25:00", "05:26:00",
"05:27:00", "05:28:00", "05:29:00", "05:30:00", "05:31:00", "05:32:00",
"05:33:00", "05:34:00", "05:35:00", "05:36:00", "05:37:00", "05:38:00",
"05:39:00", "05:40:00", "05:41:00", "05:42:00", "05:43:00", "05:44:00",
"05:45:00", "05:46:00", "05:47:00", "05:48:00", "05:49:00", "05:50:00",
"05:51:00", "05:52:00", "05:53:00", "05:54:00", "05:55:00", "05:56:00",
"05:57:00", "05:58:00", "05:59:00", "06:00:00", "06:01:00", "06:02:00",
"06:03:00", "06:04:00", "06:05:00", "06:06:00", "06:07:00", "06:08:00",
"06:09:00", "06:10:00", "06:11:00", "06:12:00", "06:13:00", "06:14:00",
"06:15:00", "06:16:00", "06:17:00", "06:18:00", "06:19:00", "06:20:00",
"06:21:00", "06:22:00", "06:23:00", "06:24:00", "06:25:00", "06:26:00",
"06:27:00", "06:28:00", "06:29:00", "06:30:00", "06:31:00", "06:32:00",
"06:33:00", "06:34:00", "06:35:00", "06:36:00", "06:37:00", "06:38:00",
"06:39:00", "06:40:00", "06:41:00", "06:42:00", "06:43:00", "06:44:00",
"06:45:00", "06:46:00", "06:47:00", "06:48:00", "06:49:00", "06:50:00",
"06:51:00", "06:52:00", "06:53:00", "06:54:00", "06:55:00", "06:56:00",
"06:57:00", "06:58:00", "06:59:00", "07:00:00", "07:01:00", "07:02:00",
"07:03:00", "07:04:00", "07:05:00", "07:06:00", "07:07:00", "07:08:00",
"07:09:00", "07:10:00", "07:11:00", "07:12:00", "07:13:00", "07:14:00",
"07:15:00", "07:16:00", "07:17:00", "07:18:00", "07:19:00", "07:20:00",
"07:21:00", "07:22:00", "07:23:00", "07:24:00", "07:25:00", "07:26:00",
"07:27:00", "07:28:00", "07:29:00", "07:30:00", "07:31:00", "07:32:00",
"07:33:00", "07:34:00", "07:35:00", "07:36:00", "07:37:00", "07:38:00",
"07:39:00", "07:40:00", "07:41:00", "07:42:00", "07:43:00", "07:44:00",
"07:45:00", "07:46:00", "07:47:00", "07:48:00", "07:49:00", "07:50:00",
"07:51:00", "07:52:00", "07:53:00", "07:54:00", "07:55:00", "07:56:00",
"07:57:00", "07:58:00", "07:59:00", "08:00:00", "08:01:00", "08:02:00",
"08:03:00", "08:04:00", "08:05:00", "08:06:00", "08:07:00", "08:08:00",
"08:09:00", "08:10:00", "08:11:00", "08:12:00", "08:13:00", "08:14:00",
"08:15:00", "08:16:00", "08:17:00", "08:18:00", "08:19:00", "08:20:00",
"08:21:00", "08:22:00", "08:23:00", "08:24:00", "08:25:00", "08:26:00",
"08:27:00", "08:28:00", "08:29:00", "08:30:00", "08:31:00", "08:32:00",
"08:33:00", "08:34:00", "08:35:00", "08:36:00", "08:37:00", "08:38:00",
"08:39:00", "08:40:00", "08:41:00", "08:42:00", "08:43:00", "08:44:00",
"08:45:00", "08:46:00", "08:47:00", "08:48:00", "08:49:00", "08:50:00",
"08:51:00", "08:52:00", "08:53:00", "08:54:00", "08:55:00", "08:56:00",
"08:57:00", "08:58:00", "08:59:00", "09:00:00", "09:01:00", "09:02:00",
"09:03:00", "09:04:00", "09:05:00", "09:06:00", "09:07:00", "09:08:00",
"09:09:00", "09:10:00", "09:11:00", "09:12:00", "09:13:00", "09:14:00",
"09:15:00", "09:16:00", "09:17:00", "09:18:00", "09:19:00", "09:20:00",
"09:21:00", "09:22:00", "09:23:00", "09:24:00", "09:25:00", "09:26:00",
"09:27:00", "09:28:00", "09:29:00", "09:30:00", "09:31:00", "09:32:00",
"09:33:00", "09:34:00", "09:35:00", "09:36:00", "09:37:00", "09:38:00",
"09:39:00", "09:40:00", "09:41:00", "09:42:00", "09:43:00", "09:44:00",
"09:45:00", "09:46:00", "09:47:00", "09:48:00", "09:49:00", "09:50:00",
"09:51:00", "09:52:00", "09:53:00", "09:54:00", "09:55:00", "09:56:00",
"09:57:00", "09:58:00", "09:59:00", "10:00:00", "10:01:00", "10:02:00",
"10:03:00", "10:04:00", "10:05:00", "10:06:00", "10:07:00", "10:08:00",
"10:09:00", "10:10:00", "10:11:00", "10:12:00", "10:13:00", "10:14:00",
"10:15:00", "10:16:00", "10:17:00", "10:18:00", "10:19:00", "10:20:00",
"10:21:00", "10:22:00", "10:23:00", "10:24:00", "10:25:00", "10:26:00",
"10:27:00", "10:28:00", "10:29:00", "10:30:00", "10:31:00", "10:32:00",
"10:33:00", "10:34:00", "10:35:00", "10:36:00", "10:37:00", "10:38:00",
"10:39:00", "10:40:00", "10:41:00", "10:42:00", "10:43:00", "10:44:00",
"10:45:00", "10:46:00", "10:47:00", "10:48:00", "10:49:00", "10:50:00",
"10:51:00", "10:52:00", "10:53:00", "10:54:00", "10:55:00", "10:56:00",
"10:57:00", "10:58:00", "10:59:00", "11:00:00", "11:01:00", "11:02:00",
"11:03:00", "11:04:00", "11:05:00", "11:06:00", "11:07:00", "11:08:00",
"11:09:00", "11:10:00", "11:11:00", "11:12:00", "11:13:00", "11:14:00",
"11:15:00", "11:16:00", "11:17:00", "11:18:00", "11:19:00", "11:20:00",
"11:21:00", "11:22:00", "11:23:00", "11:24:00", "11:25:00", "11:26:00",
"11:27:00", "11:28:00", "11:29:00", "11:30:00", "11:31:00", "11:32:00",
"11:33:00", "11:34:00", "11:35:00", "11:36:00", "11:37:00", "11:38:00",
"11:39:00", "11:40:00", "11:41:00", "11:42:00", "11:43:00", "11:44:00",
"11:45:00", "11:46:00", "11:47:00", "11:48:00", "11:49:00", "11:50:00",
"11:51:00", "11:52:00", "11:53:00", "11:54:00", "11:55:00", "11:56:00",
"11:57:00", "11:58:00", "11:59:00", "12:00:00", "12:01:00", "12:02:00",
"12:03:00", "12:04:00", "12:05:00", "12:06:00", "12:07:00", "12:08:00",
"12:09:00", "12:10:00", "12:11:00", "12:12:00", "12:13:00", "12:14:00",
"12:15:00", "12:16:00", "12:17:00", "12:18:00", "12:19:00", "12:20:00",
"12:21:00", "12:22:00", "12:23:00", "12:24:00", "12:25:00", "12:26:00",
"12:27:00", "12:28:00", "12:29:00", "12:30:00", "12:31:00", "12:32:00",
"12:33:00", "12:34:00", "12:35:00", "12:36:00", "12:37:00", "12:38:00",
"12:39:00", "12:40:00", "12:41:00", "12:42:00", "12:43:00", "12:44:00",
"12:45:00", "12:46:00", "12:47:00", "12:48:00", "12:49:00", "12:50:00",
"12:51:00", "12:52:00", "12:53:00", "12:54:00", "12:55:00", "12:56:00",
"12:57:00", "12:58:00", "12:59:00", "13:00:00", "13:01:00", "13:02:00",
"13:03:00", "13:04:00", "13:05:00", "13:06:00", "13:07:00", "13:08:00",
"13:09:00", "13:10:00", "13:11:00", "13:12:00", "13:13:00", "13:14:00",
"13:15:00", "13:16:00", "13:17:00", "13:18:00", "13:19:00", "13:20:00",
"13:21:00", "13:22:00", "13:23:00", "13:24:00", "13:25:00", "13:26:00",
"13:27:00", "13:28:00", "13:29:00", "13:30:00", "13:31:00", "13:32:00",
"13:33:00", "13:34:00", "13:35:00", "13:36:00", "13:37:00", "13:38:00",
"13:39:00", "13:40:00", "13:41:00", "13:42:00", "13:43:00", "13:44:00",
"13:45:00", "13:46:00", "13:47:00", "13:48:00", "13:49:00", "13:50:00",
"13:51:00", "13:52:00", "13:53:00", "13:54:00", "13:55:00", "13:56:00",
"13:57:00", "13:58:00", "13:59:00", "14:00:00", "14:01:00", "14:02:00",
"14:03:00", "14:04:00", "14:05:00", "14:06:00", "14:07:00", "14:08:00",
"14:09:00", "14:10:00", "14:11:00", "14:12:00", "14:13:00", "14:14:00",
"14:15:00", "14:16:00", "14:17:00", "14:18:00", "14:19:00", "14:20:00",
"14:21:00", "14:22:00", "14:23:00", "14:24:00", "14:25:00", "14:26:00",
"14:27:00", "14:28:00", "14:29:00", "14:30:00", "14:31:00", "14:32:00",
"14:33:00", "14:34:00", "14:35:00", "14:36:00", "14:37:00", "14:38:00",
"14:39:00", "14:40:00", "14:41:00", "14:42:00", "14:43:00", "14:44:00",
"14:45:00", "14:46:00", "14:47:00", "14:48:00", "14:49:00", "14:50:00",
"14:51:00", "14:52:00", "14:53:00", "14:54:00", "14:55:00", "14:56:00",
"14:57:00", "14:58:00", "14:59:00", "15:00:00", "15:01:00", "15:02:00",
"15:03:00", "15:04:00", "15:05:00", "15:06:00", "15:07:00", "15:08:00",
"15:09:00", "15:10:00", "15:11:00", "15:12:00", "15:13:00", "15:14:00",
"15:15:00", "15:16:00", "15:17:00", "15:18:00", "15:19:00", "15:20:00",
"15:21:00", "15:22:00", "15:23:00", "15:24:00", "15:25:00", "15:26:00",
"15:27:00", "15:28:00", "15:29:00", "15:30:00", "15:31:00", "15:32:00",
"15:33:00", "15:34:00", "15:35:00", "15:36:00", "15:37:00", "15:38:00",
"15:39:00", "15:40:00", "15:41:00", "15:42:00", "15:43:00", "15:44:00",
"15:45:00", "15:46:00", "15:47:00", "15:48:00", "15:49:00", "15:50:00",
"15:51:00", "15:52:00", "15:53:00", "15:54:00", "15:55:00", "15:56:00",
"15:57:00", "15:58:00", "15:59:00", "16:00:00", "16:01:00", "16:02:00",
"16:03:00", "16:04:00", "16:05:00", "16:06:00", "16:07:00", "16:08:00",
"16:09:00", "16:10:00", "16:11:00", "16:12:00", "16:13:00", "16:14:00",
"16:15:00", "16:16:00", "16:17:00", "16:18:00", "16:19:00", "16:20:00",
"16:21:00", "16:22:00", "16:23:00", "16:24:00", "16:25:00", "16:26:00",
"16:27:00", "16:28:00", "16:29:00", "16:30:00", "16:31:00", "16:32:00",
"16:33:00", "16:34:00", "16:35:00", "16:36:00", "16:37:00", "16:38:00",
"16:39:00", "16:40:00", "16:41:00", "16:42:00", "16:43:00", "16:44:00",
"16:45:00", "16:46:00", "16:47:00", "16:48:00", "16:49:00", "16:50:00",
"16:51:00", "16:52:00", "16:53:00", "16:54:00", "16:55:00", "16:56:00",
"16:57:00", "16:58:00", "16:59:00", "17:00:00", "17:01:00", "17:02:00",
"17:03:00", "17:04:00", "17:05:00", "17:06:00", "17:07:00", "17:08:00",
"17:09:00", "17:10:00", "17:11:00", "17:12:00", "17:13:00", "17:14:00",
"17:15:00", "17:16:00", "17:17:00", "17:18:00", "17:19:00", "17:20:00",
"17:21:00", "17:22:00", "17:23:00", "17:24:00", "17:25:00", "17:26:00",
"17:27:00", "17:28:00", "17:29:00", "17:30:00", "17:31:00", "17:32:00",
"17:33:00", "17:34:00", "17:35:00", "17:36:00", "17:37:00", "17:38:00",
"17:39:00", "17:40:00", "17:41:00", "17:42:00", "17:43:00", "17:44:00",
"17:45:00", "17:46:00", "17:47:00", "17:48:00", "17:49:00", "17:50:00",
"17:51:00", "17:52:00", "17:53:00", "17:54:00", "17:55:00", "17:56:00",
"17:57:00", "17:58:00", "17:59:00", "18:00:00", "18:01:00", "18:02:00",
"18:03:00", "18:04:00", "18:05:00", "18:06:00", "18:07:00", "18:08:00",
"18:09:00", "18:10:00", "18:11:00", "18:12:00", "18:13:00", "18:14:00",
"18:15:00", "18:16:00", "18:17:00", "18:18:00", "18:19:00", "18:20:00",
"18:21:00", "18:22:00", "18:23:00", "18:24:00", "18:25:00", "18:26:00",
"18:27:00", "18:28:00", "18:29:00", "18:30:00", "18:31:00", "18:32:00",
"18:33:00", "18:34:00", "18:35:00", "18:36:00", "18:37:00", "18:38:00",
"18:39:00", "18:40:00", "18:41:00", "18:42:00", "18:43:00", "18:44:00",
"18:45:00", "18:46:00", "18:47:00", "18:48:00", "18:49:00", "18:50:00",
"18:51:00", "18:52:00", "18:53:00", "18:54:00", "18:55:00", "18:56:00",
"18:57:00", "18:58:00", "18:59:00", "19:00:00", "19:01:00", "19:02:00",
"19:03:00", "19:04:00", "19:05:00", "19:06:00", "19:07:00", "19:08:00",
"19:09:00", "19:10:00", "19:11:00", "19:12:00", "19:13:00", "19:14:00",
"19:15:00", "19:16:00", "19:17:00", "19:18:00", "19:19:00", "19:20:00",
"19:21:00", "19:22:00", "19:23:00", "19:24:00", "19:25:00", "19:26:00",
"19:27:00", "19:28:00", "19:29:00", "19:30:00", "19:31:00", "19:32:00",
"19:33:00", "19:34:00", "19:35:00", "19:36:00", "19:37:00", "19:38:00",
"19:39:00", "19:40:00", "19:41:00", "19:42:00", "19:43:00", "19:44:00",
"19:45:00", "19:46:00", "19:47:00", "19:48:00", "19:49:00", "19:50:00",
"19:51:00", "19:52:00", "19:53:00", "19:54:00", "19:55:00", "19:56:00",
"19:57:00", "19:58:00", "19:59:00", "20:00:00", "20:01:00", "20:02:00",
"20:03:00", "20:04:00", "20:05:00", "20:06:00", "20:07:00", "20:08:00",
"20:09:00", "20:10:00", "20:11:00", "20:12:00", "20:13:00", "20:14:00",
"20:15:00", "20:16:00", "20:17:00", "20:18:00", "20:19:00", "20:20:00",
"20:21:00", "20:22:00", "20:23:00", "20:24:00", "20:25:00", "20:26:00",
"20:27:00", "20:28:00", "20:29:00", "20:30:00", "20:31:00", "20:32:00",
"20:33:00", "20:34:00", "20:35:00", "20:36:00", "20:37:00", "20:38:00",
"20:39:00", "20:40:00", "20:41:00", "20:42:00", "20:43:00", "20:44:00",
"20:45:00", "20:46:00", "20:47:00", "20:48:00", "20:49:00", "20:50:00",
"20:51:00", "20:52:00", "20:53:00", "20:54:00", "20:55:00", "20:56:00",
"20:57:00", "20:58:00", "20:59:00", "21:00:00", "21:01:00", "21:02:00",
"21:03:00", "21:04:00", "21:05:00", "21:06:00", "21:07:00", "21:08:00",
"21:09:00", "21:10:00", "21:11:00", "21:12:00", "21:13:00", "21:14:00",
"21:15:00", "21:16:00", "21:17:00", "21:18:00", "21:19:00", "21:20:00",
"21:21:00", "21:22:00", "21:23:00", "21:24:00", "21:25:00", "21:26:00",
"21:27:00", "21:28:00", "21:29:00", "21:30:00", "21:31:00", "21:32:00",
"21:33:00", "21:34:00", "21:35:00", "21:36:00", "21:37:00", "21:38:00",
"21:39:00", "21:40:00", "21:41:00", "21:42:00", "21:43:00", "21:44:00",
"21:45:00", "21:46:00", "21:47:00", "21:48:00", "21:49:00", "21:50:00",
"21:51:00", "21:52:00", "21:53:00", "21:54:00", "21:55:00", "21:56:00",
"21:57:00", "21:58:00", "21:59:00", "22:00:00", "22:01:00", "22:02:00",
"22:03:00", "22:04:00", "22:05:00", "22:06:00", "22:07:00", "22:08:00",
"22:09:00", "22:10:00", "22:11:00", "22:12:00", "22:13:00", "22:14:00",
"22:15:00", "22:16:00", "22:17:00", "22:18:00", "22:19:00", "22:20:00",
"22:21:00", "22:22:00", "22:23:00", "22:24:00", "22:25:00", "22:26:00",
"22:27:00", "22:28:00", "22:29:00", "22:30:00", "22:31:00", "22:32:00",
"22:33:00", "22:34:00", "22:35:00", "22:36:00", "22:37:00", "22:38:00",
"22:39:00", "22:40:00", "22:41:00", "22:42:00", "22:43:00", "22:44:00",
"22:45:00", "22:46:00", "22:47:00", "22:48:00", "22:49:00", "22:50:00",
"22:51:00", "22:52:00", "22:53:00", "22:54:00", "22:55:00", "22:56:00",
"22:57:00", "22:58:00", "22:59:00", "23:00:00", "23:01:00", "23:02:00",
"23:03:00", "23:04:00", "23:05:00", "23:06:00", "23:07:00", "23:08:00",
"23:09:00", "23:10:00", "23:11:00", "23:12:00", "23:13:00", "23:14:00",
"23:15:00", "23:16:00", "23:17:00", "23:18:00", "23:19:00", "23:20:00",
"23:21:00", "23:22:00", "23:23:00", "23:24:00", "23:25:00", "23:26:00",
"23:27:00", "23:28:00", "23:29:00", "23:30:00", "23:31:00", "23:32:00",
"23:33:00", "23:34:00", "23:35:00", "23:36:00", "23:37:00", "23:38:00",
"23:39:00", "23:40:00", "23:41:00", "23:42:00", "23:43:00", "23:44:00",
"23:45:00", "23:46:00", "23:47:00", "23:48:00", "23:49:00", "23:50:00",
"23:51:00", "23:52:00", "23:53:00", "23:54:00", "23:55:00", "23:56:00",
"23:57:00", "23:58:00", "23:59:00"), class = "factor")
Use [[ instead of [ and it will work:
file[[2]] <- strptime(file[[2]], "%X")
The result:
[1] "2014-06-04 17:24:00" "2014-06-04 17:25:00"
[3] "2014-06-04 17:26:00" "2014-06-04 17:27:00"
[5] "2014-06-04 17:28:00" "2014-06-04 17:29:00"
By the way, "%X" is a short alternative for "%H:%M:%S".

Calculating Time Weighted Rate of Return in R

Is there an R function or library that will give me the monthly (or any other specified timeframe) time weighted rate of return (twrr) for my portfolio?
I am including a dput dump of sample data below of the date and portfolio ending balance below. Not sure why the dates were dput'ed the way they were, but the first date 12053 is '2003-01-01' and the last date 12195 is '2003-05-23'.
portfolio.df <- structure(
list(
Date = structure(c(12053, 12054, 12055, 12058,
12059, 12060, 12061, 12062, 12065, 12066, 12067, 12068, 12069,
12073, 12074, 12075, 12076, 12079, 12080, 12081, 12082, 12083,
12086, 12087, 12088, 12089, 12090, 12093, 12094, 12095, 12096,
12097, 12101, 12102, 12103, 12104, 12107, 12108, 12109, 12110,
12111, 12114, 12115, 12116, 12117, 12118, 12121, 12122, 12123,
12124, 12125, 12128, 12129, 12130, 12131, 12132, 12135, 12136,
12137, 12138, 12139, 12142, 12143, 12144, 12145, 12146, 12149,
12150, 12151, 12152, 12153, 12156, 12157, 12158, 12159, 12163,
12164, 12165, 12166, 12167, 12170, 12171, 12172, 12173, 12174,
12177, 12178, 12179, 12180, 12181, 12184, 12185, 12186, 12187,
12188, 12191, 12192, 12193, 12194, 12195),
class = "Date"),
Ending_Balance = c(56250000L,
56852500L, 57080000L, 57355000L, 57477500L, 56817500L, 57885000L,
57810000L, 57732500L, 57670000L, 57520000L, 57285000L, 57270000L,
56655000L, 55802500L, 56337500L, 55642500L, 54510000L, 54987500L,
55802500L, 56065000L, 56865000L, 56635000L, 56497500L, 56640000L,
56155000L, 55757500L, 55972500L, 55865000L, 55535000L, 55885000L,
56840000L, 56902500L, 56945000L, 56622500L, 57012500L, 57200000L,
58072500L, 57612500L, 57447500L, 57157500L, 57032500L, 57405000L,
57502500L, 56785000L, 57007500L, 56342500L, 55697500L, 56655000L,
56900000L, 57002500L, 57465000L, 57467500L, 57382500L, 57982500L,
56562500L, 58065000L, 58935000L, 58502500L, 58200000L, 57767500L,
57757500L, 58055000L, 58305000L, 58277500L, 58295000L, 59047500L,
58907500L, 59125000L, 59072500L, 59107500L, 59315000L, 59690000L,
58957500L, 59407500L, 59385000L, 59965000L, 60297500L, 59890000L,
59822500L, 60367500L, 60407500L, 60380000L, 60815000L, 61155000L,
61080000L, 61132500L, 61265000L, 60912500L, 61107500L, 61445000L,
61345000L, 61137500L, 61035000L, 60707500L, 61340000L, 61365000L,
61402500L, 61640000L, 61675000L)),
.Names = c("Date", "Ending_Balance"),
row.names = c(NA, 100L),
class = "data.frame")

Resources