Invalid type (list) for variable X, where X is the date class

I have a csv file that looks like this (note that it uses ;

both column separator and ,

decimal marker):

Values;Date.Col
911;20/12/2013 04:05:01 p.m.
124,82;23/12/2013 09:43:03 a.m.
287;23/12/2013 09:44:15 a.m.
37,3;23/12/2013 09:45:26 a.m.
448,4;23/12/2013 09:46:50 a.m.
432,5;23/12/2013 09:48:31 a.m.

      

Anyway, I'm interested in plotting the behavior Values

over time, so I run these commands (being "tmp2.csv" the filename):

df <- read.table("tmp2.csv", header = T, dec = ',', sep = ';')
df$Date.Col <- strptime(df$Date.Col, "%d/%m/%Y %I:%M:%S %p")
str(df)
plot(Values ~ Date.Col, df, type = 'l')

      

Everything seems to be fine until the last command, which gives the following error message:

Error in (function (formula, data = NULL, subset = NULL, na.action = na.fail,  : 
  invalid type (list) for variable 'Date.Col'

      

I have fixed numeric variables against date class vectors in the past, so I assumed that R could handle such a situation, but maybe the problem is with the presence of hours, minutes and seconds.

Searching for similar errors in the lists and SO did not provide any useful information, does anyone know what exactly the problem is, or what I can do to solve it?

Thanks in advance.

+3


source to share


1 answer


Following the suggestion to use as.POSIXct

, I ended up with this (note that I am picking up the first line df

in the plot command in order to better illustrate how the plot works fine):

df <- read.table("tmp2.csv", header = T, dec = ',', sep = ';')
df$Date.Col <- as.POSIXct(df$Date.Col, format = "%d/%m/%Y %I:%M:%S %p")
plot(Values ~ Date.Col, df[-1,], type = 'l')

      



Thanks everyone.

+2


source







All Articles