如何将datetime从十进制转换为“%d。%m。%y%H:%M:%S”? [英] How to elegantly convert datetime from decimal to "%d.%m.%y %H:%M:%S"?
问题描述
41149.014850
的时间戳。我想将这个十进制时间戳转换为 28.08.2012 00:21:23
。我最好在R中最好的做这个? 我尝试使用函数 strsplit
以及函数<$具有指定来源的c $ c> as.Date 以及 times
函数。但无济于事将时间戳分成两个可以使用函数 as.Date
和次
的函数进行分割的问题。 / p>
这是一些演示代码:
myDatetime< - c 41149.004641,#28.08.2012 00:06:41
41149.009745,#28.08.2012 00:14:02
41149.014850,#28.08.2012 00:21:23
41149.019954)#28.08。 2012 00:28:44
##不为我工作
Dat.char< - as.character(myDatetime)
date.split< - strsplit(Dat .char,split =\\。)
##如何从这里继续,如果它是一个好方法
如果您需要更多关于挑战的信息,或者如果我能以更好的方式提出问题,请通知我。任何搜索都是无效的。
非常感谢直接帮助或链接到其他网站/帖子。
您的日期采用Excel格式的日期格式(1900年1月1日之后),因此您需要将其转换为R日格式。然后,您可以将其转换为日期时间格式( POSIXct
)。
#first convert to R Date
/ pre>
datetime< - as.Date(myDatetime-1,origin =1899-12-31)
#现在转换为POSIXct
(posixct < .POSIXct(unslass(datetime)* 86400,tz =GMT))
#[1]2012-08-28 00:06:40 GMT2012-08-28 00:14:01 GMT
#[3]2012-08-28 00:21:23 GMT2012-08-28 00:28:44 GMT
#次有时候关闭1秒,添加更多数字到秒可以看到为什么
选项(digits.secs = 6)
posixct
#[1]2012-08-28 00:06:40.9823 GMT2012-08-28 00:14:01.9680 GMT
#[3]2012-08-28 00:21:23.0399 GMT2012-08-28 00:28:44.0256 GMT
#round to nearest second
(posixct< - round(posixct,sec))
#[1]2012-08-28 00:06:41 GMT2012-08-28 00:14:02 GMT
#[3]2012-08-28 00:21:23 GMT2012-08-28 00:28:44 GMT
#现在你可以转换成你想要的格式
格式(posixct,%d。%m。%Y%H:%M:%S)
#[1]28.08.2012 00:06:41 28.08.2012 00:14:02
#[3]28.08.2012 00:21:2328.08.2012 00:28:44
I am doing an experiment which produces automatically logged data. The software produces a timestamp that is of the format
41149.014850
. I would like to convert this decimal timestamp to28.08.2012 00:21:23
. How can I do this in R most elegantly?I tried using the function
strsplit
and also the functionas.Date
with a specified origin, and also thetimes
function. But to no avail. I have problems in splitting the timestamp into two numbers that I can access with the functionsas.Date
andtimes
.Here is some demo code:
myDatetime <- c(41149.004641, # 28.08.2012 00:06:41 41149.009745, # 28.08.2012 00:14:02 41149.014850, # 28.08.2012 00:21:23 41149.019954) # 28.08.2012 00:28:44 ## not working out for me Dat.char <- as.character(myDatetime) date.split <- strsplit(Dat.char, split = "\\.") ## how to proceed from here, if it is a good way at all
Please let me know if you need more information about the challenge or if I can pose the question in a better way. Any searching was to no avail.
Direct help or linkage to other websites/posts is greatly appreciated.
解决方案You dates are in an Excel-like date format (days after January 1, 1900), so you need to convert them to an R date format. Then you can convert it to a datetime format (
POSIXct
).# first convert to R Date datetime <- as.Date(myDatetime-1, origin="1899-12-31") # now convert to POSIXct (posixct <- .POSIXct(unclass(datetime)*86400, tz="GMT")) # [1] "2012-08-28 00:06:40 GMT" "2012-08-28 00:14:01 GMT" # [3] "2012-08-28 00:21:23 GMT" "2012-08-28 00:28:44 GMT" # times are sometimes off by 1 second, add more digits to seconds to see why options(digits.secs=6) posixct # [1] "2012-08-28 00:06:40.9823 GMT" "2012-08-28 00:14:01.9680 GMT" # [3] "2012-08-28 00:21:23.0399 GMT" "2012-08-28 00:28:44.0256 GMT" # round to nearest second (posixct <- round(posixct, "sec")) # [1] "2012-08-28 00:06:41 GMT" "2012-08-28 00:14:02 GMT" # [3] "2012-08-28 00:21:23 GMT" "2012-08-28 00:28:44 GMT" # now you can convert to your desired format format(posixct, "%d.%m.%Y %H:%M:%S") # [1] "28.08.2012 00:06:41" "28.08.2012 00:14:02" # [3] "28.08.2012 00:21:23" "28.08.2012 00:28:44"
这篇关于如何将datetime从十进制转换为“%d。%m。%y%H:%M:%S”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!