|||
以.csv格式储存的通量数据为例,这些半小时时间分辨率的通量数据,通常是以第一列数据作为ID。R读取这一列数据时会遇到各种情况。如何读成R里面的时间格式的呢?以下是我用到的几种方法,供大家参考,希望有一种适合你的数据。
方法一:
# 如果你遇到这样的时间数据
DT <- c("2014/4/11 8:00:00","2014/4/11 8:30:00")
class(DT)
# "character"
library(timeDate)
DT.new <- timeDate (DT, format="%Y/%m/%d %H:%M:%S")
class(DT.new)
# "timeDate"
# OK,解决了
# 如果你遇到这样的时间数据
DT <- c("2014-4-11 8:00:00","2014/4/11 8:30:00") # / 成了 -
class(DT)
# "character"
DT.new <- timeDate (DT, format="%Y-%m-%d %H:%M:%S")
class(DT.new)
# "timeDate"
# OK,解决了
# timeDate 里面有好用的时间处理函数
dayOfYear(DT.new)
# 101 101
# 计算day of year
# 方法二
# 自己写
DT2 <- seq(strptime('2014/4/11 0:00:00',"%Y/%m/%d %H:%M:%S"), by = "30 mins", length.out = 48)
# 2014/4/11 0:00:00 是数据开始的时间,by = "30 mins"表示数据的频率,length.out = 48表示数据的长度
class(DT2)
#"POSIXct" "POSIXt"
DT2
#[1] "2014-04-11 00:00:00 CST" "2014-04-11 00:30:00 CST" "2014-04-11 01:00:00 CST"
#[4] "2014-04-11 01:30:00 CST" "2014-04-11 02:00:00 CST" "2014-04-11 02:30:00 CST"
#[7] "2014-04-11 03:00:00 CST" "2014-04-11 03:30:00 CST" "2014-04-11 04:00:00 CST"
#[10] "2014-04-11 04:30:00 CST" "2014-04-11 05:00:00 CST" "2014-04-11 05:30:00 CST"
#[13] "2014-04-11 06:00:00 CST" "2014-04-11 06:30:00 CST" "2014-04-11 07:00:00 CST"
#[16] "2014-04-11 07:30:00 CST" "2014-04-11 08:00:00 CST" "2014-04-11 08:30:00 CST"
#[19] "2014-04-11 09:00:00 CST" "2014-04-11 09:30:00 CST" "2014-04-11 10:00:00 CST"
#[22] "2014-04-11 10:30:00 CST" "2014-04-11 11:00:00 CST" "2014-04-11 11:30:00 CST"
#[25] "2014-04-11 12:00:00 CST" "2014-04-11 12:30:00 CST" "2014-04-11 13:00:00 CST"
#[28] "2014-04-11 13:30:00 CST" "2014-04-11 14:00:00 CST" "2014-04-11 14:30:00 CST"
#[31] "2014-04-11 15:00:00 CST" "2014-04-11 15:30:00 CST" "2014-04-11 16:00:00 CST"
#[34] "2014-04-11 16:30:00 CST" "2014-04-11 17:00:00 CST" "2014-04-11 17:30:00 CST"
#[37] "2014-04-11 18:00:00 CST" "2014-04-11 18:30:00 CST" "2014-04-11 19:00:00 CST"
#[40] "2014-04-11 19:30:00 CST" "2014-04-11 20:00:00 CST" "2014-04-11 20:30:00 CST"
#[43] "2014-04-11 21:00:00 CST" "2014-04-11 21:30:00 CST" "2014-04-11 22:00:00 CST"
#[46] "2014-04-11 22:30:00 CST" "2014-04-11 23:00:00 CST" "2014-04-11 23:30:00 CST"
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-24 06:40
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社