Search code examples
rdata-cleaning

How to prevent date from getting distorted after manipulation in r


The data was manipulated multiple times in R and exported as csv or xlsx format.

In excel the original date looks normal: (time = c("2016-08-10 09:30:01", "2016-08-11 09:30:01", "2016-08-12 09:30:01", "2016-08-13 09:30:01", "2016-08-14 09:30:01", "2016-08-15 09:30:01"), but after reading into R again and plotted in graphs, it got distorted and looks like 0001-01-16, 0001-01-16, 0001-02-16 on the graph.

How could I change the date and make it normal? Thank you very much.


Solution

  • If you parse this input correctly, things should work. Here we use anytime() from the anytime package which is robust to input types and formats:

    R> library(anytime)
    R> time <- c("2016-08-10 09:30:01", "2016-08-11 09:30:01", "2016-08-12 09:30:01", 
    +            "2016-08-13 09:30:01", "2016-08-14 09:30:01", "2016-08-15 09:30:01")
    R> anytime(time)
    [1] "2016-08-10 09:30:01 CDT" "2016-08-11 09:30:01 CDT" "2016-08-12 09:30:01 CDT"
    [4] "2016-08-13 09:30:01 CDT" "2016-08-14 09:30:01 CDT" "2016-08-15 09:30:01 CDT"
    R> 
    

    These are now POSIXct objects so you can use them to plot, analyse, summarize, ...

    R> diff(anytime(time))
    Time differences in days
    [1] 1 1 1 1 1
    R> 
    

    The comment by @42 is still correct though: your question is little lacking in specifics and detail.