Combining date and time into a Date column for plotting

前端 未结 2 1717
孤独总比滥情好
孤独总比滥情好 2020-12-19 02:03

I want to create a line plot. I have 3 columns in my data frame:

date        time   numbers
01-02-2010  14:57  5
01-02-2010  23:23  7
02-02-2010  05:05  3
02         


        
2条回答
  •  再見小時候
    2020-12-19 02:45

    Reconstruct your data:

    dat <- read.table(text="
    date        time   numbers
    01-02-2010  14:57  5
    01-02-2010  23:23  7
    02-02-2010  05:05  3
    02-02-2010  10:23  11", header=TRUE)
    

    Now use as.POSIXct() and paste() to combine your date and time into a POSIX date. You need to specify the format, using the symbols defined in ?strptime. Also see ?DateTimeClasses for more information

    dat$newdate <- with(dat, as.POSIXct(paste(date, time), format="%m-%d-%Y %H:%M"))
    plot(numbers ~ newdate, data=dat, type="b", col="blue")
    

    enter image description here

提交回复
热议问题