精美的代码可在R中给定时间段生成等距的时间戳 [英] Elegant code to generate equally spaced time stamps for given period in R
本文介绍了精美的代码可在R中给定时间段生成等距的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中的给定时间段内,为每个四分之一小时生成时间戳列表的一种优雅方法是什么。
该函数应该具有一年
as.POSIXct(strptime( 2015-11-14 01 :30:00,%Y-%m-%d%H:%M:%S),tz = UTC)
达到特定年份的四分之一小时。
解决方案
这是一个简单易用的功能,我认为在指定日期和时间时总是显得有些笨拙。
count.up = function(year){
seq.POSIXt(from = as.POSIXct(strptime(paste0 (年份,-01-01 00:00:00),%Y-%m-%d%H:%M:%S),tz = UTC),
to = as .POSIXct(strptime(paste0(year, -12-31 23:45:00),%Y-%m-%d%H:%M:%S),tz = UTC),
by = 15分钟)
}
并运行:
x = count.up(2015)
这将生成一个向量,而不是列表(根据您的要求),但是很容易将其包装在 as.list()
中进行转换。 / p>
What is an elegant way to generate a list of time-stamps for each quarter of an hour for a given period in R.
The function should have a year as input (e.g. 2015) and as give as output analig of
as.POSIXct(strptime("2015-11-14 01:30:00", "%Y-%m-%d %H:%M:%S"),tz = "UTC")
for reach quarter of an hour of the specific year.
解决方案
Here's a straight forward function, I think it'll always look a bit clumsy as you're specifying date and time.
count.up = function(year){
seq.POSIXt(from=as.POSIXct(strptime(paste0(year, "-01-01 00:00:00"), "%Y-%m-%d %H:%M:%S"),tz = "UTC"),
to=as.POSIXct(strptime(paste0(year, "-12-31 23:45:00"), "%Y-%m-%d %H:%M:%S"),tz = "UTC"),
by="15 min")
}
And run with:
x = count.up(2015)
This generates a vector rather than list (as you ask for), but it's easily wrapped in as.list()
to convert.
这篇关于精美的代码可在R中给定时间段生成等距的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文