将数字转换为 R 中的日期
convert a number to date in R
我正在对两个日期执行 ifelse 操作,结果日期是一个数字。我的具体问题是如何将此数字转换为日期格式。
下面是造成这个问题的代码,变量是fecha_epi_final:
library(lubridate)
library(dplyr)
temp_epi2 <- temp_epi1 %>%
ungroup() %>%
mutate (b1 = force_tz(b1, tzone = "GMT"),
b2 = b2 - days(1),
fecha_epi_final = ifelse (is.na(b2) == T, b1 ,
ifelse (b1>=b2 , b2 , b1)),
mes_epi_inicio = floor_date(fecha_epi_inicio, "month"))
fecha_epi_final的str是一个数字,例如:1417564800
作为测试数据框使用:
library(lubridate)
library(dplyr)
b1 <- c('2015-01-30' , '2014-12-17')
b2 <- c(NA , '2014-12-15')
temp_epi1 <- data.frame(b1 , b2) %>%
mutate(b1 = ymd(b1) ,
b2 = ymd(b2))
为什么不简单:
as.POSIXlt(1417564800, origin="1970-01-01", tz="America/New_York")
# [1] "2014-12-02 19:00:00 EST"
我正在对两个日期执行 ifelse 操作,结果日期是一个数字。我的具体问题是如何将此数字转换为日期格式。
下面是造成这个问题的代码,变量是fecha_epi_final:
library(lubridate)
library(dplyr)
temp_epi2 <- temp_epi1 %>%
ungroup() %>%
mutate (b1 = force_tz(b1, tzone = "GMT"),
b2 = b2 - days(1),
fecha_epi_final = ifelse (is.na(b2) == T, b1 ,
ifelse (b1>=b2 , b2 , b1)),
mes_epi_inicio = floor_date(fecha_epi_inicio, "month"))
fecha_epi_final的str是一个数字,例如:1417564800
作为测试数据框使用:
library(lubridate)
library(dplyr)
b1 <- c('2015-01-30' , '2014-12-17')
b2 <- c(NA , '2014-12-15')
temp_epi1 <- data.frame(b1 , b2) %>%
mutate(b1 = ymd(b1) ,
b2 = ymd(b2))
为什么不简单:
as.POSIXlt(1417564800, origin="1970-01-01", tz="America/New_York")
# [1] "2014-12-02 19:00:00 EST"