Frage

I have this 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)

I start by converting the "y" factor variable into POSIXct with lubridate as follows:

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

Then I generate the reference data for the calculations and proceed

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

and I am getting a strange result with julian days to high numbers

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

Some of the years are working fine, and that is a common procedure I have been using in diferent data frames

Any idea?

Thanks!!

War es hilfreich?

Lösung

The difference that you are getting is measured in seconds.

> 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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top