作者Wush978 (拒看低质媒体)
看板R_Language
标题Re: [问题] 请问R能否直接读懂"时间"格式?
时间Sat Nov 2 14:08:45 2013
时间资料要用正确的资料型态, 这里我用POSIXlt当范例
把这些资料储存成`data.txt`:
: 2008-04-07 17:33:48|1730.05
: 2008-04-08 12:28:13|1222.02
: 2008-04-09 12:26:23|1215.51
: 2008-04-11 12:41:00|1248.12
: 2008-04-12 14:01:15|1398.85
: 2008-04-14 12:37:15|1235.13
: 2008-04-15 12:24:22|1220.12
: 2008-04-15 17:33:48|1742.14
: 2008-04-16 12:23:52|1220.32
: 2008-04-17 12:35:46|1240.22
: [程式范例]:
```r
src.df <- read.table("data.txt", sep="|")
colnames(src.df) <- c("time", "value")
src.df$value <- as.numeric(src.df$value)
src.df$time <- strptime(src.df$time, "%Y-%m-%d %H:%M:%S")
src.df$sec <- unclass(src.df$time)$sec
src.df$min <- unclass(src.df$time)$min
src.df$hr <- unclass(src.df$time)$hour
src.df$dtime <- src.df$hr * 3600 + src.df$min * 60 + src.df$sec
plot(value ~ dtime, src.df)
```
这可能是你要的
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.166.97.140
1F:推 goodparent1:我有类似的问题,好像也是资料本身非正确的型态。 11/02 15:42
2F:→ goodparent1:会出现error in xts : requires an appropriate 11/02 15:43
3F:→ goodparent1:time-based object 这是为什麽? 11/02 15:44
4F:→ celestialgod:Class要对...不然他会认为只是字串 11/02 15:51
5F:推 mystea:谢谢, 这确实是我要的. 11/03 09:26
6F:→ goodparent1:我读入资料x,用指令 11/03 17:17
7F:→ goodparent1:indexClass(x) <- c("POSIXt", "POSIXct") 11/03 17:17
8F:→ goodparent1:该指令是错的,我该怎麽写比较好? 11/03 17:19
9F:→ goodparent1:还是需要完整的资料格式跟code比较好判断? 11/03 17:19