My question has been asked and answered repeatedly here in Stackoverflow but none of the solutions seem to work for me. Could you please help? Here is a sample of my data using 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))
As you see, There are two date columns which look similar in excel (the cell format for both is date: *14-03-2012
. However, when I open the file in R, Date1 is of type Dttm while Date2 is character. I can get my desired date format for Date 1 using the code below:
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
But I can't get the second Date column in my desired format.If I use the code above it introduces many NAs. I also used this code openxlsx::convertToDateTime(mydata$Date2)
leading to the following error: NAs introduced by coercion
.
Then I tried this:
library(tibble)
library(janitor)
excel_numeric_to_date(as.numeric(as.character(mydata$Date2), date_system = "modern"))
which led to this warning message:
In excel_numeric_to_date(as.numeric(as.character(mydata$Date2), :
NAs introduced by coercion
I also tried this:
library("datetimeutils")
convert_date(mydata$Date2, type = "Excel")
Error in charToDate(x) :
character string is not in a standard unambiguous format
Could somebody please help me?
you have a problem of brackets and an as.character
to much here. Does the following work? excel_numeric_to_date(as.numeric(mydata$Date2), date_system = "modern")
I don't get errors with: as.POSIXct(as.numeric(t$Date2),origin = '1970-01-01 00:00.00 UTC')
but I don't know the correct origin.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.