1

I have a data.frame "df" such as:

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

I am trying to plot "T1", "T2", "T3" against "Time" using solution provided here

But the following error occurs:

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 Answer 1

2

POSIXlt values are actually lists, and that's the problem. You can see this with

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" ...

If you convert them to POSIXct values, you should be fine.

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
# ...
# ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.