{"id":667,"date":"2014-10-14T11:12:44","date_gmt":"2014-10-14T18:12:44","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=667"},"modified":"2014-10-14T11:12:44","modified_gmt":"2014-10-14T18:12:44","slug":"how-to-convert-specific-time-format-to-timestamp-in-r","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/10\/14\/how-to-convert-specific-time-format-to-timestamp-in-r\/","title":{"rendered":"How to convert specific time format to timestamp in R?"},"content":{"rendered":"<div id=\"question\" class=\"question\" data-questionid=\"19062178\">\n<table>\n<tbody>\n<tr>\n<td class=\"postcell\">\n<div class=\"post-text\">\n<div class=\"question-status question-originals-of-duplicate\">\n<p>This question already has an answer here:<\/p>\n<ul>\n<li><a dir=\"ltr\" href=\"http:\/\/stackoverflow.com\/questions\/3554572\/read-csv-with-dates-and-numbers-with-r-statistical-software\">read csv with dates and numbers with R statistical software<\/a> <span class=\"question-originals-answer-count\">3 answers<\/span><\/li>\n<\/ul>\n<\/div>\n<p>I am working on &#8220;Localization Data for Person Activity Data Set&#8221; dataset from UCI and in this data set there is a column of date and time(both in one column) with following format:<\/p>\n<pre class=\"lang-r prettyprint prettyprinted\"><code><span class=\"lit\">27.05.2009<\/span> <span class=\"lit\">14<\/span><span class=\"pun\">:<\/span><span class=\"lit\">03<\/span><span class=\"pun\">:<\/span><span class=\"lit\">25<\/span><span class=\"pun\">:<\/span><span class=\"lit\">777<\/span>\n<span class=\"lit\">27.05.2009<\/span> <span class=\"lit\">14<\/span><span class=\"pun\">:<\/span><span class=\"lit\">03<\/span><span class=\"pun\">:<\/span><span class=\"lit\">25<\/span><span class=\"pun\">:<\/span><span class=\"lit\">183<\/span>\n<span class=\"lit\">27.05.2009<\/span> <span class=\"lit\">14<\/span><span class=\"pun\">:<\/span><span class=\"lit\">03<\/span><span class=\"pun\">:<\/span><span class=\"lit\">25<\/span><span class=\"pun\">:<\/span><span class=\"lit\">210<\/span>\n<span class=\"lit\">27.05.2009<\/span> <span class=\"lit\">14<\/span><span class=\"pun\">:<\/span><span class=\"lit\">03<\/span><span class=\"pun\">:<\/span><span class=\"lit\">25<\/span><span class=\"pun\">:<\/span><span class=\"lit\">237<\/span>\n<span class=\"lit\">...<\/span><\/code><\/pre>\n<p>I am wondering if there is anyway to convert this column to timestamp using R.<\/p>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"special-status\" colspan=\"2\">\n<div class=\"question-status\">\n<table>\n<tbody>\n<tr>\n<td class=\"votecell\">\n<div class=\"vote\"><\/div>\n<\/td>\n<td class=\"answercell\">\n<div class=\"post-text\">\n<p>First of all, we need to substitute the colon separating the milliseconds from the seconds to a dot, otherwise the final step won&#8217;t work (thanks to <a href=\"http:\/\/stackoverflow.com\/users\/143305\/dirk-eddelbuettel\">Dirk Eddelbuettel<\/a> for this one). Since in the end R will use the separators it wants, to be quicker, I&#8217;ll just go ahead and substitute all the colons for dots:<\/p>\n<pre class=\"lang-r prettyprint prettyprinted\"><code><span class=\"pln\">x <\/span><span class=\"pun\">&lt;-<\/span> <span class=\"str\">\"27.05.2009 14:03:25:777\"<\/span>  <span class=\"com\"># this is a simplified version of your data<\/span><span class=\"pln\">\ny <\/span><span class=\"pun\">&lt;-<\/span><span class=\"pln\"> gsub<\/span><span class=\"pun\">(<\/span><span class=\"str\">\":\"<\/span><span class=\"pun\">,<\/span> <span class=\"str\">\".\"<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> x<\/span><span class=\"pun\">)<\/span>          <span class=\"com\"># this is your vector with the aforementioned substitution<\/span><\/code><\/pre>\n<p>By the way, this is how your vector should look after <code>gsub<\/code>:<\/p>\n<pre class=\"lang-r prettyprint prettyprinted\"><code><span class=\"pun\">&gt;<\/span><span class=\"pln\"> y\n<\/span><span class=\"pun\">[<\/span><span class=\"lit\">1<\/span><span class=\"pun\">]<\/span> <span class=\"str\">\"27.05.2009 14.03.25.777\"<\/span><\/code><\/pre>\n<p>Now, in order to have it show the milliseconds, you first need to adjust an R option and then use a function called <code>strptime<\/code>, which will convert your date vector to POSIXlt (an R-friendly) format. Just do the following:<\/p>\n<pre class=\"lang-r prettyprint prettyprinted\"><code><span class=\"pun\">&gt;<\/span><span class=\"pln\"> options<\/span><span class=\"pun\">(<\/span><span class=\"pln\">digits.secs <\/span><span class=\"pun\">=<\/span> <span class=\"lit\">3<\/span><span class=\"pun\">)<\/span>           <span class=\"com\"># this tells R you want it to consider 3 digits for seconds.<\/span>\n<span class=\"pun\">&gt;<\/span><span class=\"pln\"> strptime<\/span><span class=\"pun\">(<\/span><span class=\"pln\">y<\/span><span class=\"pun\">,<\/span> <span class=\"str\">\"%d.%m.%Y %H:%M:%OS\"<\/span><span class=\"pun\">)<\/span>  <span class=\"com\"># this finally formats your vector<\/span>\n<span class=\"pun\">[<\/span><span class=\"lit\">1<\/span><span class=\"pun\">]<\/span> <span class=\"str\">\"2009-05-27 14:03:25.777\"<\/span><\/code><\/pre>\n<p>I&#8217;ve learned this nice trick <a href=\"http:\/\/stackoverflow.com\/a\/2150179\/1169233\">here<\/a>. <a href=\"http:\/\/stackoverflow.com\/a\/2150357\/1169233\">This other answer<\/a> also says you can skip the <code>options<\/code> setting and use, for example, <code>strptime(y, \"%d.%m.%Y %H:%M:%OS3\")<\/code>, but it doesn&#8217;t work for me. <a href=\"http:\/\/stackoverflow.com\/users\/1851712\/henrik\">Henrik<\/a> noted that the<a href=\"http:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/strptime.html\" rel=\"nofollow\">function&#8217;s help page, <code>?strptime<\/code><\/a> states that the <code>%OS3<\/code> bit is OS-dependent. I&#8217;m using an updated Ubuntu 13.04 and using <code>%OS3<\/code> yields <code>NA<\/code>.<\/p>\n<p>When using <code>strptime<\/code> (or other POSIX-related functions such as <code>as.Date<\/code>), keep in mind some of the most common conversions used (edited for brevity, as suggested by <a href=\"http:\/\/stackoverflow.com\/users\/1855677\/dwin\">DWin<\/a>. Complete list at <a href=\"http:\/\/stat.ethz.ch\/R-manual\/R-devel\/library\/base\/html\/strptime.html\" rel=\"nofollow\"><code>strptime<\/code><\/a>):<\/p>\n<ul>\n<li><code>%a<\/code> Abbreviated weekday name in the current locale.<\/li>\n<li><code>%A<\/code> Full weekday name in the current locale.<\/li>\n<li><code>%b<\/code> Abbreviated month name in the current locale.<\/li>\n<li><code>%B<\/code> Full month name in the current locale.<\/li>\n<li><code>%d<\/code> Day of the month as decimal number (01\u201331).<\/li>\n<li><code>%H<\/code> Hours as decimal number (00\u201323). Times such as 24:00:00 are accepted for input.<\/li>\n<li><code>%I<\/code> Hours as decimal number (01\u201312).<\/li>\n<li><code>%j<\/code> Day of year as decimal number (001\u2013366).<\/li>\n<li><code>%m<\/code> Month as decimal number (01\u201312).<\/li>\n<li><code>%M<\/code> Minute as decimal number (00\u201359).<\/li>\n<li><code>%p<\/code> AM\/PM indicator in the locale. Used in conjunction with <code>%I<\/code> and not with <code>%H<\/code>.<\/li>\n<li>`%S Second as decimal number (00\u201361), allowing for up to two leap-seconds (but POSIX-compliant implementations will ignore leap seconds).<\/li>\n<li><code>%U<\/code> Week of the year as decimal number (00\u201353) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.<\/li>\n<li><code>%w<\/code> Weekday as decimal number (0\u20136, Sunday is 0).<\/li>\n<li><code>%W<\/code> Week of the year as decimal number (00\u201353) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1). The UK convention.<\/li>\n<li><code>%y<\/code> Year without century (00\u201399). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19<\/li>\n<li><code>%Y<\/code> Year with century. Note that whereas there was no zero in the original Gregorian calendar, ISO 8601:2004 defines it to be valid (interpreted as 1BC)<\/li>\n<\/ul>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This question already has an answer here: read csv with dates and numbers with R statistical software 3 answers I am working on &#8220;Localization Data&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-667","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}