我有一個資料集,并且有一些日期和小時屬性。這是示例,然后我會解釋我想要做什么;
| 日期1 | 1小時 | 日期2 | 小時 2 | 日期3 | 3小時 |
|---|---|---|---|---|---|
| 2014-03-16 00:00:00 | 16:20:00 | 2014-03-16 00:00:00 | 20:20:03 | 2014-03-16 00:00:00 | 22:12:34 |
| 2014-04-22 00:00:00 | 10:20:00 | 2014-04-22 00:00:00 | 15:20:03 | 2014-04-22 00:00:00 | 20:12:34 |
| 2015-03-12 00:00:00 | 16:20:00 | 2015-03-12 00:00:00 | 20:20:03 | 2015-03-12 00:00:00 | 22:12:34 |
我們知道event1 在 event2 之前發生 (event1 -> event2 -> event3)
但是如您所見,在日期屬性中,時間部分是不正確的,但我們每個都有小時屬性。我想做的事; 我想通過使用小時屬性來更正它們,然后找到這兩個日期之間的差異并創建新的屬性,以小時為單位提供時差。
上表樣品;
| event2_time |
|---|
| 4 |
| 5 |
| 4 |
我試圖合并到目前為止的小時并創建一個像這樣的新屬性,但它不起作用。(我的目標實際上是糾正日期值并擺脫小時屬性)
trainTable <- trainTable %>%
mutate("newParam" = as.POSIXct(paste(alert_date, alert_hour), format="%Y-%m-%d %H:%M:%S")
我可以使用一些幫助,提前致謝。
資料
structure(list(alert_date = structure(c(1394928000, 1395014400,
1395014400, 1395187200, 1395273600, 1395014400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), alert_hour = c("16:15:00", "20:53:00", "12:55:00",
"14:22:00", "12:07:00", "17:48:00"), firstInterv_date = structure(c(1394928000,
1395014400, 1395014400, 1395187200, 1395273600, 1395014400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), firstInterv_hour = c("16:35:00", "21:05:00", "13:10:00",
"14:42:00", "12:07:00", "18:08:00"), extinction_date = structure(c(1394928000,
1395014400, 1395014400, 1395187200, 1395273600, 1395014400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), extinction_hour = c("17:47:00", "22:46:00", "15:30:00",
"15:25:00", "13:14:00", "21:10:00")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
uj5u.com熱心網友回復:
嘗試使用此解決方案mapply。它用于strsplit將日期與小時分開。
dat <- as.data.frame( dat ) # tibbles are cool but sometimes very restrictive, so changing to data.frame here
dat_new <- data.frame( setNames( mapply( function(x,y){
tmp <- sapply( strsplit( as.character(dat[,x]), " "), function(z) z[1] );
list( as.POSIXct( paste(tmp,dat[,y] ) ) ) },
grep("date", colnames(dat)), grep("hour", colnames(dat)) ),
c("a","b","c") ) )
dat_new$b - dat_new$a
Time differences in secs
[1] 1200 720 900 1200 0 1200
# if you need tibbles convert back if you need
as_tibble( dat_new )
# A tibble: 6 x 3
a b c
<dttm> <dttm> <dttm>
1 2014-03-16 16:15:00 2014-03-16 16:35:00 2014-03-16 17:47:00
2 2014-03-17 20:53:00 2014-03-17 21:05:00 2014-03-17 22:46:00
3 2014-03-17 12:55:00 2014-03-17 13:10:00 2014-03-17 15:30:00
4 2014-03-19 14:22:00 2014-03-19 14:42:00 2014-03-19 15:25:00
5 2014-03-20 12:07:00 2014-03-20 12:07:00 2014-03-20 13:14:00
6 2014-03-17 17:48:00 2014-03-17 18:08:00 2014-03-17 21:10:00
資料
dat <- structure(list(alert_date = structure(c(1394928000, 1395014400,
1395014400, 1395187200, 1395273600, 1395014400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), alert_hour = c("16:15:00", "20:53:00", "12:55:00",
"14:22:00", "12:07:00", "17:48:00"), firstInterv_date = structure(c(1394928000,
1395014400, 1395014400, 1395187200, 1395273600, 1395014400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), firstInterv_hour = c("16:35:00", "21:05:00", "13:10:00",
"14:42:00", "12:07:00", "18:08:00"), extinction_date = structure(c(1394928000,
1395014400, 1395014400, 1395187200, 1395273600, 1395014400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), extinction_hour = c("17:47:00", "22:46:00", "15:30:00",
"15:25:00", "13:14:00", "21:10:00")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/390738.html
