R融合函数ERROR:数学函数的非数字参数

时间:2015-03-05 05:29:09

标签: r ggplot2 reshape2

我有一个data.frame“df”,例如:

df
                  Time    T1    T2   T3
2015-02-20 04:00:00.00 128.0 136.5 27.6
2015-02-20 04:00:00.25 128.1 136.3 27.7
2015-02-20 04:00:00.50 128.3 136.2 27.7
2015-02-20 04:00:00.75 128.5 136.1 27.7
2015-02-20 04:00:01.00 128.6 136.1 27.7
2015-02-20 04:00:01.25 129.0 135.7 27.7
2015-02-20 04:00:01.50 129.1 135.6 27.8
2015-02-20 04:00:01.75 129.3 135.5 27.8
2015-02-20 04:00:02.00 129.5 135.5 27.8
2015-02-20 04:00:02.25 129.8 135.4 27.8

 str(df)
#'data.frame':   10 obs. of  4 variables:
# $ Time: POSIXlt, format: "2015-02-20 04:00:00.00" "2015-02-20 04:00:00.25" "2015-02-20 04:00:00.50" "2015-02-20 04:00:00.75" ...
# $ T1  : num  128 128 128 128 129 ...
# $ T2  : num  136 136 136 136 136 ...
# $ T3  : num  27.6 27.7 27.7 27.7 27.7 27.7 27.8 27.8 27.8 27.8

我正在尝试使用提供的解决方案{“3}}

对”时间“绘制”T1“,”T2“,”T3“

但发生以下错误:

mdf <- melt(df, id.vars="Time")
mdf
Error in round(secs, i) : non-numeric argument to mathematical function
In addition: Warning message:
In is.na(secs) : is.na() applied to non-(list or vector) of type 'NULL'

1 个答案:

答案 0 :(得分:2)

POSIXlt值实际上是列表,这就是问题所在。你可以用

看到这个
is.list(df$Time)
# [1] TRUE
ls.str(df$Time)
# gmtoff :  int [1:10] NA NA NA NA NA NA NA NA NA NA
# hour :  int [1:10] 4 4 4 4 4 4 4 4 4 4
# isdst :  int [1:10] 0 0 0 0 0 0 0 0 0 0
# mday :  int [1:10] 20 20 20 20 20 20 20 20 20 20
# min :  int [1:10] 0 0 0 0 0 0 0 0 0 0
# mon :  int [1:10] 1 1 1 1 1 1 1 1 1 1
# sec :  num [1:10] 0 0 0 0 1 1 1 1 2 2
# wday :  int [1:10] 5 5 5 5 5 5 5 5 5 5
# yday :  int [1:10] 50 50 50 50 50 50 50 50 50 50
# year :  int [1:10] 115 115 115 115 115 115 115 115 115 115
# zone :  chr [1:10] "PST" "PST" "PST" "PST" "PST" ...

如果将它们转换为POSIXct值,则应该没问题。

library(reshape2)    

df$Time <- as.POSIXct(df$Time)
melt(df, id.vars = "Time")
#                   Time variable value
# 1  2015-02-20 04:00:00       T1 128.0
# 2  2015-02-20 04:00:00       T1 128.1
# 3  2015-02-20 04:00:00       T1 128.3
# 4  2015-02-20 04:00:00       T1 128.5
# 5  2015-02-20 04:00:01       T1 128.6
# ...
# ...