我有一个xts对象,x.我想根据索引中的时间戳运行for循环直到某个时间.
> index(x)
[1] "2011-10-12 16:44:00 SAST"
Run Code Online (Sandbox Code Playgroud)
假设只要索引中的时间小于16:40:00,我就想运行循环.考虑到上面索引的格式,如何去掉时间组件?
这应该能让你到达你想去的地方。使用format,您可以仅提取小时、分钟和秒部分。帮助页面?strptime提供了有关用于提取信息的符号的更多详细信息。
#if you don't have your string in an appropriate format yet
(x <- strptime("2011-10-12 16:44:00 SAST", format = "%Y-%m-%d %H:%M:%S"))
[1] "2011-10-12 16:44:00"
class(x)
[1] "POSIXlt" "POSIXt"
(new.x <- format(x, format = "%H:%M:%S"))
[1] "16:44:00"
Run Code Online (Sandbox Code Playgroud)