如何使用lubridate从开始时间转换和截断时间戳?

时间:2014-12-31 17:10:21

标签: r timestamp lubridate

我想将数据集的时间戳转换为更可行的东西,并且是lubridate / R的新手。

数据最初来自%d/%m/%Y %H:%M,并希望从开始时间将其更改为%d %H

我想将数据截断为DMY_H,然后将其重新排列为原点后的小时和天

以下是数据样本:

time    dht22_t dht11_t dht22_h dht11_h db  pa  treatment_hive  wifi
01/09/2014 15:19    NA      NA  NA      NA  51.75467    NA      0   1
01/09/2014 15:20    30.8    31  59.8    44  55.27682    100672  0   1
01/09/2014 15:21    30.8    31  60.3    44  54.81995    100675  0   1
01/09/2014 15:22    30.8    31  60.9    44  54.14134    100671  0   1
01/09/2014 15:23    30.8    31  61.1    44  53.88574    100672  0   1
01/09/2014 15:24    30.8    31  61.2    44  53.68800    100680  0   1

谢谢! 编辑:

代码:

df$time<-format(ymd_hms(df$time), '%d %H:%M:%S') 

更改了开始时间或来源的天数,但不是小时分钟数。 理想情况下,它看起来像这样:

    time DHT22_t DHT11_t DHT22_h DHT11_h       db      pa hive_id treatment_hive wifi
01 15:00:00      NA      NA      NA      NA 51.75467      NA       1              0    1
01 16:00:00    30.8      31    59.8      44 55.27682 100.672       1              0    1
01 17:00:00    30.8      31    60.3      44 54.81995 100.675       1              0    1

编辑:

> dput(droplevels(head(Hive2)))
structure(list(time = structure(1:6, .Label = c("2014-09-01 15:25:05", 
"2014-09-01 15:25:09", "2014-09-01 15:25:11", "2014-09-01 15:25:15", 
"2014-09-01 15:25:18", "2014-09-01 15:25:20"), class = "factor"), 
    DHT22_t = c(0, 0, 0, 0, 0, 0), DHT11_t = c(0L, 31L, 31L, 
    31L, 31L, 31L), DHT22_h = c(0, 0, 0, 0, 0, 0), DHT11_h = c(0L, 
    51L, 53L, 53L, 52L, 50L), db = c(60.8, 59.4, 60.4, 59.2, 
    60.3, 60.2), kPa = c(NA, 100.798, 100.792, 100.791, 100.79, 
    100.791), hive_id = c(2L, 2L, 2L, 2L, 2L, 2L), treatment_hive = c(1L, 
    1L, 1L, 1L, 1L, 1L), wifi = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("time", 
"DHT22_t", "DHT11_t", "DHT22_h", "DHT11_h", "db", "kPa", "hive_id", 
"treatment_hive", "wifi"), row.names = c(NA, 6L), class = "data.frame")

代码:

dt1 <- ymd_hms(Hive2$time)
v1 <- times(format(dt1, '%H:%M:%S'))
dt2<- paste(format(dt1, '%d'), v1-v1[1])
> dput(head(dt2))
c("01  0.000000e+00", "01  4.629630e-05", "01  6.944444e-05", 
"01  1.157407e-04", "01  1.504630e-04", "01  1.736111e-04")

和此:

dt2.1<- paste(format(dt1, '%d %H:%M:%S'), v1-v1[1])
> dput(head(dt2.1))
c("01 15:25:05  0.000000e+00", "01 15:25:09  4.629630e-05", "01 15:25:11 6.944444e-05", 
 "01 15:25:15  1.157407e-04", "01 15:25:18  1.504630e-04", "01 15:25:20  1.736111e-04"
 )

1 个答案:

答案 0 :(得分:1)

你可以尝试

 library(lubridate)
 library(chron)
 dt1 <- dmy_hm(df$time)

注意:根据提供的示例,时间为15:19:0015:20:00。使用dput显示数据集的一个原因。

  dt1
 #[1] "2014-09-01 15:19:00 UTC" "2014-09-01 15:20:00 UTC"
 #[3] "2014-09-01 15:21:00 UTC" "2014-09-01 15:22:00 UTC"
 #[5] "2014-09-01 15:23:00 UTC" "2014-09-01 15:24:00 UTC"

 v1 <- times(format(dt1, '%H:%M:%S')) 
 paste(format(dt1, '%d'), v1-v1[1])
 #[1] "01 00:00:00" "01 00:01:00" "01 00:02:00" "01 00:03:00" "01 00:04:00"
 #[6] "01 00:05:00"

更新

基于更新的数据集“Hive2”

dt1 <- ymd_hms(Hive2$time)
v1 <- times(format(dt1, '%H:%M:%S'))
v1
#[1] 15:25:05 15:25:09 15:25:11 15:25:15 15:25:18 15:25:20
paste(format(dt1, '%d'), v1-v1[1])
#[1] "01 00:00:00" "01 00:00:04" "01 00:00:06" "01 00:00:10" "01 00:00:13"
#[6] "01 00:00:15"

数据

 df <- structure(list(time = c("01/09/2014 15:19", "01/09/2014 15:20", 
 "01/09/2014 15:21", "01/09/2014 15:22", "01/09/2014 15:23", "01/09/2014 15:24"
 ), dht22_t = c(NA, 30.8, 30.8, 30.8, 30.8, 30.8), dht11_t = c(NA, 
 31L, 31L, 31L, 31L, 31L), dht22_h = c(NA, 59.8, 60.3, 60.9, 61.1, 
 61.2), dht11_h = c(NA, 44L, 44L, 44L, 44L, 44L), db = c(51.75467, 
 55.27682, 54.81995, 54.14134, 53.88574, 53.688), pa = c(NA, 100672L, 
 100675L, 100671L, 100672L, 100680L), treatment_hive = c(0L, 0L, 
 0L, 0L, 0L, 0L), wifi = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("time", 
 "dht22_t", "dht11_t", "dht22_h", "dht11_h", "db", "pa", "treatment_hive", 
 "wifi"), class = "data.frame", row.names = c(NA, -6L))