Excel中类似列的日期在R中的导入方式不同,如何获得所需的日期格式:%d-%m-%Y?

xzv2uavs  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(155)

我的问题在Stackoverflow中已经被反复地问过和回答过了,但是似乎没有一个解决方案对我有效。你能帮忙吗?下面是我使用dput的数据示例。dput(sample_n(CA_Complication,20))

structure(list(ID = c("101", "101", "101", "101", "101", "101", 
"101", "101", "101", "101", "101", "101", "101", "101", "101", 
"101", "101", "101", "101", "101"), AD = c("447", "243", "608", 
"537", "1588", "302", "28", "1035", "755", "8", "1262", "95", 
"1954", "231", "88", "1898", "1028", "669", "103", "773"), Date1 = structure(c(1367802000, 
1324515600, 1404781200, 1391043600, 1621299600, 1340067600, 1291078800, 
1489453200, 1456966800, 1278986400, 1544058000, 1295398800, 1637629200, 
1321318800, 1293411600, 1623286800, 1485392400, 1417050000, 1296522000, 
1431997200), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    Date2 = c("41400.041666666701", "40913.041666666701", 
    "41828.041666666701", "41697.041666666701", "44334.041666666701", 
    "41080.041666666701", "40512.041666666701", "42901.041666666701", 
    "42443.041666666701", "40372.041666666701", "43535.041666666701", 
    "40197.041666666701", "44523.041666666701", "40862.041666666701", 
    "40539.041666666701", "44357.041666666701", "42817.041666666701", 
    "42016.041666666701", "40575.041666666701", "42143.041666666701"
    ), procedure = c("CH-0447", "CH-0243", "CH-0608", 
    "CH-0537", "CH-1588", "CH-0302", "CH-0028", 
    "CH-1035", "CH-0755", "CH-0008", "CH-1262", 
    "CH-0095", "CH-1954", "CH-0231", "CH-0088", 
    "CH-1898", "CH-1028", "CH-0669", "CH-0103", 
    "CH-0773"), PAT = c("101-447", "101-243", "101-608", 
    "101-537", "101-1588", "101-302", "101-28", "101-1035", "101-755", 
    "101-8", "101-1262", "101-95", "101-1954", "101-231", "101-88", 
    "101-1898", "101-1028", "101-669", "101-103", "101-773"), 
    LOMP17 = c(NA, NA, NA, NA, 309015, NA, NA, 409030, NA, NA, 
    209015, NA, 209005, NA, NA, 209005, NA, NA, NA, NA), LOMP = c(30055, 
    140020, 60020, 10005, NA, 90005, 30005, NA, 10005, NA, NA, 
    10005, NA, 30005, 20005, NA, NA, 10005, 30060, 100010)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L))

如您所见,Excel中有两个日期列(单元格格式均为date: *14-03-2012)。但是,当我在R中打开文件时,Date1的类型为Dttm,而Date2的类型为character。我可以使用以下代码为Date1获取所需的日期格式:

library("anytime")  
anydate(mydata$Date1)     #yeilds date as yyyy-mm-dd,hh:mm:ss
mydata$Date1 <- substring(mydata$Date1,1,10)     # Remove hh:mm:ss
mydata$Date1 <- format(as.Date(mydata$Date1,'%Y-%m-%d'),'%d-%m-%Y')
class(mydata$Date1)       #character

但是我无法得到第二个日期列的格式。如果我使用上面的代码,它会引入许多NA。我还使用了这个代码openxlsx::convertToDateTime(mydata$Date2),导致以下错误:NAs introduced by coercion.
然后我试了这个:

library(tibble)
library(janitor)
excel_numeric_to_date(as.numeric(as.character(mydata$Date2), date_system = "modern"))

这导致了这个警告消息:

In excel_numeric_to_date(as.numeric(as.character(mydata$Date2),  :
NAs introduced by coercion

我也试过这个:

library("datetimeutils")
convert_date(mydata$Date2, type = "Excel")
Error in charToDate(x) : 
character string is not in a standard unambiguous format

有人能帮帮我吗?

nr7wwzry

nr7wwzry1#

这里有一个括号和一个as.character的问题。下面的工作吗?excel_numeric_to_date(as.numeric(mydata$Date2), date_system = "modern")
我没有收到以下错误:as.POSIXct(as.numeric(t$Date2),origin = '1970-01-01 00:00.00 UTC'),但我不知道正确的原点。

相关问题