问题
I have a date column that is numeric as follows
Date1
4010
5178
5494
6750
7106
39
39
172
1704
4152
I am trying to convert this to real date using this function
as.Date(df$Date1, origin = "1970-01-01")
However I am seeing that these numbers are converted to these dates which are incorrect.
Date1_Converted
1980-12-24
1984-03-26
1985-01-16
1988-06-25
1989-06-16
1970-02-09
1970-02-09
1970-06-22
1974-09-01
1981-05-15
The correct transformation should have been
Date1_CrctTrnsf
2005.10.31
2009.02.11
2009.12.04
2013.05.15
2014.05.06
1994.02.22
1994.02.22
1994.08.03
1999.05.03
2006.03.22
I am assuming this an origin issue, not sure how to fix this, any help on how to fix this issue is much appreciated.
回答1:
# First I copied the data from your question
df <- read.table(con <-file("clipboard"), header = T)
df
# Convert the format
df1 <- as.Date(df$Date1, origin = "1994-11-08")
df1
> as.data.frame(gsub("-", ".", df1))
gsub("-", ".", df1)
1 2005.10.31
2 2009.01.11
3 2009.11.23
4 2013.05.02
5 2014.04.23
6 1994.12.17
7 1994.12.17
8 1995.04.29
9 1999.07.09
10 2006.03.22
Note, however, that there seems to be an error in the data (or at least the results you are expecting to get from the data -- how did you produce these numbers?):
- The input for row 6 is 39
- The input for row 7 is 39 and
- The input for row 8 is 172
172 - 39 = 133
However you expect the dates for row 6 and 7 to be 1994.02.22
and the result for row 8 to be 1994.08.03
. This is not mathematically possible, as there are 162 days between those 2 dates.
来源:https://stackoverflow.com/questions/32144343/converting-numeric-to-date-issues