Problem to convert from factor to date in a Data Frame in R?

1

Every time I try to convert a factor to a date in a DataFrame (data) it returns "NA" in the factor values, in the example I show str (data) it returns that the DATE field is a factor.

data$DATE <- as.Date(data$DATE,format="%Y/%m/%d %H")

DATE            OBJ    No
2018-09-24-00   Leo     12
2018-09-24-00   Jhon    12
2018-09-24-02   Ale     13
2018-09-24-00   Juan    13
2018-09-24-04   Duas    13
2018-09-24-05   Doi     76
2018-09-24-06   Doi     78
2018-09-24-07   Doi     80
2018-09-24-08   Doi     82
2018-09-24-09   Doi     84
2018-09-24-00   Doi     86
2018-09-24-11   Doi     88
2018-09-24-12   Doi     90
2018-09-24-13   Doi     92
2018-09-24-14   Doi     94
2018-09-24-15   Doi     96
2018-09-24-16   Doi     98
2018-09-24-17   Doi     100
2018-09-24-18   Doi     102
2018-09-24-19   Doi     104

And he returns me

DATE    OBJ No
NA  Leo     12
NA  Jhon    12
NA  Ale     13
NA  Juan    13
NA  Duas    13
NA  Doi     76
NA  Doi     78
NA  Doi     80
NA  Doi     82
NA  Doi     84
NA  Doi     86
NA  Doi     88
NA  Doi     90
NA  Doi     92
NA  Doi     94
NA  Doi     96
NA  Doi     98
NA  Doi    100
NA  Doi    102
NA  Doi    104

This is what I need to shoot in the DataFrame for the hours of the day

data <- filter(data ,hour(Date) == 0)

And give me back

DATE    OBJ           No
2018-09-24-00   Leo   12
2018-09-24-00   Jhon  12
2018-09-24-00   Juan  13

If you can help me to cast this column DATE to full date

    
asked by Leonar Bode 25.09.2018 в 22:06
source

1 answer

1

Usually the Factor usually bring us problems, but in this case you have others:

  • The format you are using to convert to an Date object is not appropriate, it should be something like this: "%Y-%m-%d-%H" .

  • Finally, keep in mind that a Date is a date data only, it does not allow hosting the part of the time, we could do as.Date(data$DATE,format="%Y-%m-%d-%H") without problems, but we lose the information of the time.

  • Solution:

    You could work with a base object POSIXlt or POSIXct that allow you to store the time information, for example: data$DATE <- as.POSIXlt(data$DATE,format="%Y-%m-%d-%H") . However, I see that you are using the lubridate package, so I recommend that you directly use the function corresponding to the format: ymd_h() :

    data$DATE <- ymd_h(data$DATE)
    data[hour(data$DATE) == 0,]
    
             DATE  OBJ No
    1  2018-09-24  Leo 12
    2  2018-09-24 Jhon 12
    4  2018-09-24 Juan 13
    11 2018-09-24  Doi 86
    
        
    answered by 26.09.2018 / 00:14
    source