我在列中添加了date
,hour
和minute
的数据框。我想通过某种时间戳绘制value
列。无论如何要做到这一点?
> head(fl)
date hour min value
1 2014-02-23 0 0 81
2 2014-02-23 0 1 65
3 2014-02-23 0 2 73
4 2014-02-23 0 3 81
5 2014-02-23 0 4 89
6 2014-02-23 0 5 69
...
现在我正在使用ggplot2,但它将每小时和每天的分钟结合在一起:(
ggplot( fl, aes( min, value) ) + geom_line()
有什么想法吗?
答案 0 :(得分:3)
as.POSIXct
替代方案与@RobertKrzyzanowski提供相同的结果
fl <- data.frame(date=c('2014-02-23', '2014-02-22'), hour = c(0,0), min = c(1,2))
fl$stamp <- with(fl, as.POSIXct( paste(date,hour,min), format="%Y-%m-%d %H %M"))
#> fl
# date hour min stamp
#1 2014-02-23 0 1 2014-02-23 00:01:00
#2 2014-02-22 0 2 2014-02-22 00:02:00
答案 1 :(得分:1)
library(lubridate)
fl$datetime <- ymd_hm(paste(fl$date,fl$hour,fl$min,sep='-'))
ggplot(fl, aes( datetime, value) ) + geom_line()
答案 2 :(得分:1)
尝试功能ISOdatetime(Year, Month, Day, Hour, Min, Sec)
:
fl <- data.frame(date=c('2014-02-23', '2014-02-22'), hour = c(0,0), min = c(1,2))
zip <- function(x) do.call(Map, append(list(c), x))
args <- unname(append(zip(strsplit(as.character(fl$date), '-')), list(fl$hour, fl$min, 0)))
fl$timestamp <- do.call(ISOdatetime, args)
print(fl)
# date hour min timestamp
# 1 2014-02-23 0 1 2014-02-23 00:01:00
# 2 2014-02-12 0 2 2014-02-12 00:02:00
答案 3 :(得分:0)
df<- within(df[5:6], { DT=format(as.POSIXct(paste(df$date, df$time, sep = ' ')),
"%Y-%m-%d %H:%M:%S")
})