{"id":2757,"date":"2026-05-19T01:06:23","date_gmt":"2026-05-19T08:06:23","guid":{"rendered":"https:\/\/lukemiller.org\/?p=2757"},"modified":"2026-05-19T01:06:23","modified_gmt":"2026-05-19T08:06:23","slug":"handling-midnight-timestamps-with-missing-time-values-in-r","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2026\/05\/handling-midnight-timestamps-with-missing-time-values-in-r\/","title":{"rendered":"Handling midnight timestamps with missing time values in R"},"content":{"rendered":"\n<p>If you take a set of POSIXct-format time stamps in R and convert them to text strings (via as.character() ), the midnight time stamps will be shown with just the date and no 00:00 time value. If you subsequently try to convert a vector of text time stamps back to POSIXct, R may decide that your midnights aren&#8217;t midnights because they lack a 00:00 value, so you might want to add it back. <\/p>\n\n\n\n<p>This little function should accomplish that:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> # Function to add 00:00 to text representations of midnight timestamps. Input times should be text strings (characters)\nfixMidnight = function(times){\n\tifelse(grepl(\"^\\\\d{4}-\\\\d{2}-\\\\d{2}$\", tstamps), \n\tpaste0(tstamps,\" 00:00\"),\n\ttstamps)\n}<\/code><\/pre>\n\n\n\n<p>You supply it a vector of character time values (like &#8220;2024-09-10 23:00&#8221; and &#8220;2024-09-11&#8221;), and this will return character values where the midnight time value will now read &#8220;2024-09-11 00:00&#8221;, while the times that already had hours:minutes will remain unchanged: &#8220;2024-09-10 23:00&#8221; will return exactly the same as it was supplied to the function.<\/p>\n\n\n\n<p>As an explanation, the grepl pattern:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   \"^\\\\d{4}-\\\\d{2}-\\\\d{2}$\"<\/code><\/pre>\n\n\n\n<p>is looking for the typical POSIX representation of a year-month-day, i.e. YYYY-MM-DD, and only returning entries that end after the 2-digit month, instead of those that continue with hour:minute values. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you take a set of POSIXct-format time stamps in R and convert them to text strings (via as.character() ), the midnight time stamps will be shown with just the date and no 00:00 time value. If you subsequently try to convert a vector of text time stamps back to POSIXct, R may decide that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2757","post","type-post","status-publish","format-standard","hentry","category-journal"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=2757"}],"version-history":[{"count":1,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2757\/revisions"}],"predecessor-version":[{"id":2758,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/2757\/revisions\/2758"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=2757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=2757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=2757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}