0

I am trying to create a graph with timestamps on the X axis from a csv file and can't get it to work. I've distilled my problem down to the basics as follows. Data file looks like:

"timestamp","A","B","C"
"2015-11-20T17:01:40",10,20,90
"2015-11-20T17:01:50",20,25,80
"2015-11-20T17:01:60",30,30,70

I run the following code:

df=read.csv("/tmp/example.csv")
df$timestamp = strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S")
df # Debug Output
Molten <- melt(df, id.vars= "timestamp")
Molten # Debug Output

...and get the following result...

"df" going in:

            timestamp  A  B  C
1 2015-11-20 17:01:40 10 20 90
2 2015-11-20 17:01:50 20 25 80
3 2015-11-20 17:01:60 30 30 70

"Molen" out of "melt:

            timestamp variable value
1 2015-11-20 17:01:40        A    10
2 2015-11-20 17:01:50        A    20
3 2015-11-20 17:01:60        A    30
4                <NA>        B    20
5                <NA>        B    25
6                <NA>        B    30

I don't know why those <NA>s are coming out - and that appears to be what's messing up the graphing. If I remove the strptime line, the <NA>s do not happen - which makes me believe it's related to that. Granted, in doing that my assumption is the timestamps are just treated as ordinary strings and not parsed as times.

Brad
  • 11,262
  • 8
  • 55
  • 74
  • I think you'll need `POSIXct` datetimes, not `POSIXlt`. See related discussion of this in [this answer](http://stackoverflow.com/a/16972250/2461552) – aosmith Nov 24 '15 at 18:52
  • I almost didn't understand that at all - but from what I got - I changed the line to: df$timestamp = as.POSIXct(strptime(df$timestamp, "%Y-%m-%dT%H:%M:%S")) and it seemed to work :-O – Brad Nov 24 '15 at 19:12
  • or u can change the order, melt first then format the time – sinalpha Nov 24 '15 at 22:16

0 Answers0