带有POSIXct数据的意外输出

时间:2013-02-13 19:12:21

标签: r posixct lubridate

我有这个df:

>    fechas <- c("1991-01-01", "1991-01-03", "1991-01-04", "1991-01-05", 
    +             "1991-01-08", "1991-01-09", "1991-01-11", "1991-01-13", "1991-01-15", 
    +             "1991-01-16", "1991-01-20", "1991-01-21", "1991-01-27", "1991-01-29", 
    +             "1991-02-06", "1991-02-07", "1991-02-08", "1991-02-09", "1991-02-10", 
    +             "1991-02-11", "1991-02-12", "1991-02-16")
> index <- c(1:10)
> df <- merge (index,fechas)

我首先将“y”因子变量转换为带有lubridate的POSIXct,如下所示:

library (lubridate)
df$fecha <- ymd (df$y)
220 parsed with %Y-%m-%d

str(df)
'data.frame':   220 obs. of  3 variables:
 $ x    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ y    : Factor w/ 22 levels "1991-01-01","1991-01-03",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ fecha: POSIXct, format: "1991-01-01" "1991-01-01" "1991-01-01" "1991-01-01" ...

然后我为计算生成参考数据并继续

 > start_1991 <- ymd("1991-01-01")
     1 parsed with %Y-%m-%d
    > df$jul <- (df$fecha-start_1991)+1

我得到一个奇怪的结果与朱利安天到高数字

    fecha2      jul
    1991-01-03  172801
    1991-01-04  259201
    1991-01-05  345601
    1991-01-08  604801

有些年份工作正常,这是我在不同数据框架中使用的常用程序

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您获得的差异以秒为单位。

> head(df, n = 13)
    x          y      fecha         jul
1   1 1991-01-01 1991-01-01      1 secs
2   2 1991-01-01 1991-01-01      1 secs
3   3 1991-01-01 1991-01-01      1 secs
4   4 1991-01-01 1991-01-01      1 secs
5   5 1991-01-01 1991-01-01      1 secs
6   6 1991-01-01 1991-01-01      1 secs
7   7 1991-01-01 1991-01-01      1 secs
8   8 1991-01-01 1991-01-01      1 secs
9   9 1991-01-01 1991-01-01      1 secs
10 10 1991-01-01 1991-01-01      1 secs
11  1 1991-01-03 1991-01-03 172801 secs
12  2 1991-01-03 1991-01-03 172801 secs
13  3 1991-01-03 1991-01-03 172801 secs