将数字转换为日期问题 [英] Converting numeric to date issues

查看:141
本文介绍了将数字转换为日期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字列,如下所示

  Date1 
4010
5178
5494
6750
7106
39
39
172
1704
4152
/ pre>

我正在尝试使用此功能将其转换为实际日期

  as.Date(df $ Date1,origin =1970-01-01)

但是我看到这些数字被转换成这些日期不正确。

  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

正确的转换应该是

  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

我假设这是一个原因问题,不知道如何解决这个问题,任何有关如何解决这个问题的帮助是非常感谢的。 / p>

解决方案

 #首先我从你的问题复制了数据
df< read.table(con< -file(clipboard),header = T)
df

#转换格式
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

然而,请注意,那里似乎是数据中的一个错误(或至少您期望从数据获得的结果 - 您是如何生成这些数字的)?




  • 第6行的输入是 39

  • 第7行的输入是 39

  • 第8行的输入是 172



172 - 39 = 133



但是,您预计第6和7行的日期为$ code> 1994.02.22 第8行的结果为 1994.08.03 。这不是数学上可能的,因为这两个日期之间有162天。




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.

解决方案

# 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.

这篇关于将数字转换为日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆