变量X的类型(列表)无效,其中X是日期类 [英] Invalid type (list) for variable X, where X is a date class
问题描述
;
作为列分隔符和,
十进制标记): 值; Date.Col
911; 20/12/2013 04:05:01 pm
124,82; 23/12/2013 09:43:03 am
287; 23/12/2013 09:44:15 am
37,3; 23/12/2013 09 :45:26 am
448,4; 23/12/2013 09:46:50 am
432,5; 23/12/2013 09:48:31 am
无论如何,我有兴趣通过时间调整值
的行为,所以我执行这些命令(为tmp2.csv文件的名称):
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')
一切似乎都很顺利,直到最后一个命令出现以下错误消息:
错误(函数(公式,数据),$ = NULL,subset = NULL,na.action = na.fail,
变量'Date.Col'的无效类型(list)
过去,我将数值变量反映到日期类的向量,所以我假设R可以处理这种情况,但是问题在于存在数小时,分钟和秒。
在列表中寻找类似的错误,SO没有产生任何有用的见解,有谁知道这里是什么问题,或者我可以做些什么来解决? / p>
提前感谢
按照建议使用 as.POSIXct
,最后我用这个
代码(注意我把第一行 df
绘图命令,在
中,以更好地说明它的工作原理如何):
df< - 读.table(tmp2.csv,header = T,dec =',',sep =';')
df $ Date.Col< - as.POSIXct(df $ Date.Col,format = %d /%m /%Y%I:%M:%S%p)
图(值〜Date.Col,df [-1,],type ='l')
感谢大家。
I have a csv file that looks like this (note that it uses ;
as column separator and ,
as 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 ploting the behaviour of Values
through time, so I execute these commands (being "tmp2.csv" the name of the file):
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 go well 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'
In the past I ploted numeric variables against vectors of date class so I assumed that R can handle this kind of situation, but maybe the problem lies with the presence of hours, minutes and seconds.
Looking for similar errors in lists and SO didn't yield any useful insight, does anyone know what's exactly the problem here, or what can I do to solve it?
Thanks in advance.
Following the suggestion to use as.POSIXct
, I ended up with this
code (note that I take away the first row of df
in the plot command, in
order to illustrate better how the plot it's working alright):
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 to everyone.
这篇关于变量X的类型(列表)无效,其中X是日期类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!