時間の差を計算するときのはまりメモ
Rは2時点間の差によしなに単位をつけてくれる。
time0 <- ymd_hms("2018-11-11 00:00:00")
time1 <- ymd_hms("2018-11-11 11:11:11")
time2 <- ymd_hms("2018-12-31 23:59:59")
time1 - time0
## Time difference of 11.18639 hours
time2 - time0
## Time difference of 50.99999 days
処理開始時刻と測定時刻をログしたhourスケールの実験exp_hour
とdayスケールの実験exp_day
がある。
exp_hour <- tibble(init = time0, time = time1, dif = time - init)
exp_day <- tibble(init = time0, time = time2, dif = time - init)
これをwrite_csv
-> read_csv
とするとdouble型に落ちて、実験間で単位に齟齬が生じる。
temp_file1 <- tempfile()
temp_file2 <- tempfile()
write_csv(exp_hour, path = temp_file1)
write_csv(exp_day, path = temp_file2)
exp_hour2 <- read_csv(temp_file1)
exp_day2 <- read_csv(temp_file2)
exp_hour2$dif
## [1] 11.18639
exp_day2$dif
## [1] 50.99999
実用的には、
as.numeric(units = "hours")
で単位を指定するlubridate
のduration
系関数 (dhours
など) で割って単位変換する
round(50.9999) * 24
## [1] 1224
exp_day %>% transmute(dif = as.numeric(dif, units = "hours")) %$% dif
## [1] 1224
exp_day %>% transmute(dif = dif / dhours(1)) %$% dif
## [1] 1224
ついでに、単位の異なる時間差をまとめて扱う場合、差をとる順序によって結果が変わってくる。
(exp_hour %<>% select(-dif))
## # A tibble: 1 x 2
## init time
## <dttm> <dttm>
## 1 2018-11-11 00:00:00 2018-11-11 11:11:11
(exp_day %<>% select(-dif))
## # A tibble: 1 x 2
## init time
## <dttm> <dttm>
## 1 2018-11-11 00:00:00 2018-12-31 23:59:59
# さきに差を計算
sub_before <-
bind_rows(
exp_hour %>% transmute(dif = time - init),
exp_day %>% transmute(dif = time - init)
)
sub_before
## # A tibble: 2 x 1
## dif
## <time>
## 1 " 40271 secs"
## 2 4406399 secs
# あとで差を計算
sub_after <-
bind_rows(exp_hour, exp_day) %>%
transmute(dif = time - init)
sub_after
## # A tibble: 2 x 1
## dif
## <time>
## 1 " 11.18639 hours"
## 2 1223.99972 hours
mutate
では生成される列内で最小の単位に統一されるのに対し、c
やbind_rows
でつなげると任意の単位の組み合わせからsecあるいはday単位のいずれかに換算されるのが原因。
dif_hour = ymd_hms("2018-11-11 06:00:00") - ymd_hms("2018-11-11 00:00:00")
dif_week = ymd_hms("2018-11-18 01:00:00") - ymd_hms("2018-11-11 00:00:00")
dif_month = ymd_hms("2018-12-11 01:00:00") - ymd_hms("2018-11-11 00:00:00")
dif_year = ymd_hms("2019-11-11 01:00:0") - ymd_hms("2018-11-11 00:00:00")
attributes(c(dif_hour, dif_week))$units
## [1] "secs"
attributes(c(dif_hour, dif_month))$units
## [1] "secs"
attributes(c(dif_hour, dif_year))$units
## [1] "secs"
attributes(c(dif_week, dif_month))$units
## [1] "days"
attributes(c(dif_week, dif_year))$units
## [1] "days"
attributes(c(dif_month, dif_year))$units
## [1] "days"
Session information
devtools::session_info()
## setting value
## version R version 3.5.0 (2018-04-23)
## system x86_64, darwin15.6.0
## ui X11
## language (EN)
## collate en_US.UTF-8
## tz Australia/Sydney
## date 2018-11-13
##
## package * version date source
## assertthat 0.2.0 2017-04-11 CRAN (R 3.5.0)
## backports 1.1.2 2017-12-13 CRAN (R 3.5.0)
## base * 3.5.0 2018-04-24 local
## bindr 0.1.1 2018-03-13 CRAN (R 3.5.0)
## bindrcpp * 0.2.2 2018-03-29 CRAN (R 3.5.0)
## blogdown 0.8 2018-07-15 CRAN (R 3.5.0)
## bookdown 0.7 2018-02-18 CRAN (R 3.5.0)
## broom * 0.5.0 2018-07-17 CRAN (R 3.5.0)
## cellranger 1.1.0 2016-07-27 CRAN (R 3.5.0)
## cli 1.0.1 2018-09-25 CRAN (R 3.5.0)
## colorspace 1.3-2 2016-12-14 CRAN (R 3.5.0)
## compiler 3.5.0 2018-04-24 local
## crayon 1.3.4 2017-09-16 CRAN (R 3.5.0)
## curl 3.2 2018-03-28 CRAN (R 3.5.0)
## datasets * 3.5.0 2018-04-24 local
## devtools * 1.13.6 2018-06-27 CRAN (R 3.5.0)
## digest 0.6.18 2018-10-10 cran (@0.6.18)
## dplyr * 0.7.6 2018-06-29 cran (@0.7.6)
## evaluate 0.11 2018-07-17 CRAN (R 3.5.0)
## fansi 0.4.0 2018-10-05 CRAN (R 3.5.0)
## forcats * 0.3.0 2018-02-19 CRAN (R 3.5.0)
## ggplot2 * 3.1.0 2018-10-25 CRAN (R 3.5.0)
## glue 1.3.0 2018-07-17 cran (@1.3.0)
## googleway * 2.7.1002 2018-11-11 Github (SymbolixAU/googleway@a9bbd33)
## graphics * 3.5.0 2018-04-24 local
## grDevices * 3.5.0 2018-04-24 local
## grid 3.5.0 2018-04-24 local
## gtable 0.2.0 2016-02-26 CRAN (R 3.5.0)
## haven 1.1.2 2018-06-27 CRAN (R 3.5.0)
## hms 0.4.2 2018-03-10 CRAN (R 3.5.0)
## htmltools 0.3.6 2017-04-28 CRAN (R 3.5.0)
## htmlwidgets 1.3 2018-09-30 CRAN (R 3.5.0)
## httpuv 1.4.5 2018-07-19 CRAN (R 3.5.0)
## httr 1.3.1 2017-08-20 CRAN (R 3.5.0)
## jsonlite 1.5 2017-06-01 CRAN (R 3.5.0)
## knitr * 1.20 2018-02-20 CRAN (R 3.5.0)
## later 0.7.5 2018-09-18 CRAN (R 3.5.0)
## lattice 0.20-35 2017-03-25 CRAN (R 3.5.0)
## lazyeval 0.2.1 2017-10-29 CRAN (R 3.5.0)
## lubridate * 1.7.4 2018-04-11 CRAN (R 3.5.0)
## magrittr * 1.5 2014-11-22 CRAN (R 3.5.0)
## MASS * 7.3-50 2018-04-30 CRAN (R 3.5.0)
## memoise 1.1.0 2017-04-21 CRAN (R 3.5.0)
## methods * 3.5.0 2018-04-24 local
## mime 0.6 2018-10-05 cran (@0.6)
## modelr 0.1.2 2018-05-11 cran (@0.1.2)
## munsell 0.5.0 2018-06-12 CRAN (R 3.5.0)
## nlme 3.1-137 2018-04-07 CRAN (R 3.5.0)
## pillar 1.3.0 2018-07-14 CRAN (R 3.5.0)
## pkgconfig 2.0.2 2018-08-16 CRAN (R 3.5.0)
## plyr * 1.8.4 2016-06-08 CRAN (R 3.5.0)
## promises 1.0.1 2018-04-13 CRAN (R 3.5.0)
## purrr * 0.2.5 2018-05-29 CRAN (R 3.5.0)
## R6 2.3.0 2018-10-04 cran (@2.3.0)
## Rcpp 1.0.0 2018-11-07 cran (@1.0.0)
## readr * 1.1.1 2017-05-16 CRAN (R 3.5.0)
## readxl 1.1.0 2018-04-20 CRAN (R 3.5.0)
## rlang 0.3.0.1 2018-10-25 cran (@0.3.0.1)
## rmarkdown 1.10 2018-06-11 cran (@1.10)
## rprojroot 1.3-2 2018-01-03 CRAN (R 3.5.0)
## rstudioapi 0.8 2018-10-02 CRAN (R 3.5.0)
## rvest 0.3.2 2016-06-17 CRAN (R 3.5.0)
## scales 1.0.0 2018-08-09 CRAN (R 3.5.0)
## shiny 1.2.0 2018-11-02 cran (@1.2.0)
## stats * 3.5.0 2018-04-24 local
## stringi 1.2.4 2018-07-20 CRAN (R 3.5.0)
## stringr * 1.3.1 2018-05-10 cran (@1.3.1)
## tibble * 1.4.2 2018-01-22 CRAN (R 3.5.0)
## tidyr * 0.8.1 2018-05-18 cran (@0.8.1)
## tidyselect 0.2.4 2018-02-26 CRAN (R 3.5.0)
## tidyverse * 1.2.1 2017-11-14 CRAN (R 3.5.0)
## tools 3.5.0 2018-04-24 local
## utf8 1.1.4 2018-05-24 cran (@1.1.4)
## utils * 3.5.0 2018-04-24 local
## withr 2.1.2 2018-03-15 CRAN (R 3.5.0)
## xfun 0.3 2018-07-06 CRAN (R 3.5.0)
## xml2 1.2.0 2018-01-24 CRAN (R 3.5.0)
## xtable 1.8-3 2018-08-29 CRAN (R 3.5.0)
## yaml 2.2.0 2018-07-25 cran (@2.2.0)