[英]reshape2 melt function on timestamps gives NAs
I'm trying to melt a data frame with reshape2 melt
function, in order to plot my dataset with ggplot2.我正在尝试使用 reshape2
melt
函数熔化数据框,以便用 ggplot2 绘制我的数据集。 However, with my reference column, containing a timestamp, it gives me NA
after first iteration (timestamps are correctly displayed once each, and then I have these NA
s).但是,对于包含时间戳的我的参考列,它在第一次迭代后给了我
NA
(每个时间戳正确显示一次,然后我有这些NA
)。
Here is my dataset:这是我的数据集:
TIMESTAMP,RECNUM,Tair,Tground
2015-01-06 16:27,1,5.0,7.0
2015-01-06 16:28,2,6.0,7.0
2015-01-06 16:29,3,6.0,7.0
2015-01-06 16:30,4,6.5,7.0
2015-01-06 16:31,5,6.8,7.1
2015-01-06 16:32,6,6.8,7.1
2015-01-06 16:33,7,6.8,7.12
2015-01-06 16:34,8,7.1,7.1
2015-01-06 16:35,9,7.15,7.09
2015-01-06 16:36,10,7.18,7.1
2015-01-06 16:37,11,7.3,7.1
My R code:我的R代码:
library(ggplot2)
library(reshape2)
datafile <- file.choose()
dat <- read.csv(datafile)
dat$TIMESTAMP <- as.POSIXlt(strptime(dat$TIMESTAMP,"%Y-%m-%d %H:%M"))
meltedData = melt(dat, id.vars=c("RECNUM", "TIMESTAMP"), variable.name="Measure")
And the resulting meltedData
variable:以及由此产生的
meltedData
变量:
> meltedData
RECNUM TIMESTAMP Measure value
1 1 2015-01-06 16:27:00 Tair 5.00
2 2 2015-01-06 16:28:00 Tair 6.00
3 3 2015-01-06 16:29:00 Tair 6.00
4 4 2015-01-06 16:30:00 Tair 6.50
5 5 2015-01-06 16:31:00 Tair 6.80
6 6 2015-01-06 16:32:00 Tair 6.80
7 7 2015-01-06 16:33:00 Tair 6.80
8 8 2015-01-06 16:34:00 Tair 7.10
9 9 2015-01-06 16:35:00 Tair 7.15
10 10 2015-01-06 16:36:00 Tair 7.18
11 11 2015-01-06 16:37:00 Tair 7.30
12 1 <NA> Tground 7.00
13 2 <NA> Tground 7.00
14 3 <NA> Tground 7.00
15 4 <NA> Tground 7.00
16 5 <NA> Tground 7.10
17 6 <NA> Tground 7.10
18 7 <NA> Tground 7.12
19 8 <NA> Tground 7.10
20 9 <NA> Tground 7.09
21 10 <NA> Tground 7.10
22 11 <NA> Tground 7.10
What am I doing wrong ?我究竟做错了什么 ? Is there a way to have proper timestamps all table long ?
有没有办法让所有表都具有正确的时间戳?
Thanks,谢谢,
J. J。
Edit This question is different from this one , as my question is not oriented on how to use reshape2 to change frow wide to long, but on why do reshape2::melt
gives me NA
s on timestamps.编辑这个问题是从不同的这一个,因为我的问题不是如何使用reshape2来改变FROW宽长为导向,但为什么
reshape2::melt
给我NA
S于时间戳。
The problem with your code arises from the fact that you have coerced your TIMESTAMP
column to POSIXlt
.您的代码的问题源于您已将
TIMESTAMP
列强制为POSIXlt
。 Keep in mind that POSIXlt
and POSIXct
, while very similar from the exterior, are way different.请记住,
POSIXlt
和POSIXct
虽然从外观上非常相似,但有很大的不同。 POSIXlt
is actually a list. POSIXlt
实际上是一个列表。 Try for instance:尝试例如:
x<-as.POSIXlt("2017-01-15 15:00:00")
typeof(x)
#[1] "list"
unclass(x)
#$sec
#[1] 0
#$min
#[1] 0
#$hour
#[1] 15
#$mday
#[1] 15
#...
As you can see, a POSIXlt
object is a list
with elements indicating the seconds, minutes, hour and so on of a date.如您所见,
POSIXlt
对象是一个包含指示日期的秒、分、小时等元素的list
。 Let's POSIXct
:让我们
POSIXct
:
x<-as.POSIXct("2017-01-15 15:00:00")
typeof(x)
#[1] "double"
unclass(x)
#[1] 1484488800
#attr(,"tzone")
#[1] ""
POSIXct
on the other hand are just the number of seconds from the UNIX epoch and so the date can be stored as just a single number.另一方面,
POSIXct
只是 UNIX 时代的秒数,因此日期可以存储为单个数字。 POSIXct
are lighter than POSIXlt
. POSIXct
比POSIXlt
轻。
While it is possible to have a column in a data.frame
which is a list, it's much better to have POSIXct
:虽然可以在
data.frame
有一列是一个列表,但最好有POSIXct
:
dat$TIMESTAMP <- as.POSIXct(strptime(dat$TIMESTAMP,"%Y-%m-%d %H:%M"))
#this works now as intended
melt(dat, id.vars=c("RECNUM", "TIMESTAMP"), variable.name="Measure")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.