Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R数据帧格式日期,某些日期早于1900年1月1日_R_Date - Fatal编程技术网

R数据帧格式日期,某些日期早于1900年1月1日

R数据帧格式日期,某些日期早于1900年1月1日,r,date,R,Date,我正在导入带有日期列的CSV,其中一些日期早于1900年1月1日。我试图在数据框中创建一列,其中日期采用%m-%d-%Y类型的格式,但我的工作返回空单元格。下面是一个例子。谢谢你的帮助 id <- c(1,2,3) dates <- c(44321, 1, "December 25, 1890") df <- data.frame(id, dates) View(df) id你的混合格式让事情有点尴尬,但是 library(lubridate) as_d

我正在导入带有日期列的CSV,其中一些日期早于1900年1月1日。我试图在数据框中创建一列,其中日期采用%m-%d-%Y类型的格式,但我的工作返回空单元格。下面是一个例子。谢谢你的帮助

id <- c(1,2,3)
dates <- c(44321, 1, "December 25, 1890")
df <- data.frame(id, dates)
View(df)

id你的混合格式让事情有点尴尬,但是

library(lubridate)

as_date(
  ifelse(
    is.na(as.numeric(dates)), 
    mdy(dates), 
    dmy("01-Jan-1900") + days(as.numeric(dates)-1)
  )
)
[1] "2021-05-06" "1900-01-01" "1890-12-25"
这似乎是合理的

您确定要转换
44321
?[1900年和2000年都不是闰年…]


as.numeric()-44321
as.Date(“1900-01-01”)-1
将等于它,但它们不同,因此问题中似乎存在错误。长格式的44321日期来自Excel;请参阅下面Limey对此问题的评论。如果我以前没有使用lubridate,我将立即试用。我认为44321=5/5/2021,至少在Excel中是这样,所以这个解决方案可能有问题?永远不要依赖Excel!From:“微软Excel从最早的版本开始就错误地认为1900年是闰年”,这可以解释这种不一致性。
library(lubridate)

as_date(
  ifelse(
    is.na(as.numeric(dates)), 
    mdy(dates), 
    dmy("01-Jan-1900") + days(as.numeric(dates)-1)
  )
)
[1] "2021-05-06" "1900-01-01" "1890-12-25"