从R中的3个csv文件绘制温度和时间(m / d / y h:m:s AM / PM)

时间:2015-02-15 16:26:30

标签: r class datetime csv

我是初学R用户,需要做一些破坏我头脑的事情。我有3个csv文件,每个有2列:1个用于时间(用于"月/日/年小时:分钟:第二个上午/下午和#34;)和1个用于温度。 3个文件的测量时间不同。 我得到的第一个问题是,在打开csv文件时,R无法识别" Time"列作为时间(它属于"字符"的类。)

这是文件的样子(文件的名称是Noble2):

Time    Temp
1   08/20/14 02:05:02 PM    17.034
2   08/20/14 02:35:02 PM    16.749
3   08/20/14 03:05:02 PM    16.963
4   08/20/14 03:35:02 PM    16.820
5   08/20/14 04:05:02 PM    16.963
6   08/20/14 04:35:02 PM    17.153
7   08/20/14 05:05:02 PM    16.249
8   08/20/14 05:35:02 PM    15.652
9   08/20/14 06:05:02 PM    14.649
10  08/20/14 06:35:02 PM    13.906
11  08/20/14 07:05:02 PM    13.209
12  08/20/14 07:35:02 PM    12.316
13  08/20/14 08:05:02 PM    12.268
14  08/20/14 08:35:02 PM    12.243
15  08/20/14 09:05:02 PM    12.219
16  08/20/14 09:35:02 PM    12.171
17  08/20/14 10:05:02 PM    12.147
18  08/20/14 10:35:02 PM    12.122
19  08/20/14 11:05:02 PM    12.074
20  08/20/14 11:35:02 PM    12.025
21  08/21/14 12:05:02 AM    11.977
22  08/21/14 12:35:02 AM    11.929
23  08/21/14 01:05:02 AM    11.856
24  08/21/14 01:35:02 AM    11.807
25  08/21/14 02:05:02 AM    11.759

这些是命令:

  

类(Noble2 $时间)   [1]"字符"

csv文件位于:https://www.dropbox.com/s/9lljwadmnp9uvot/Noble2.csv?dl=0

非常感谢你的时间!

1 个答案:

答案 0 :(得分:0)

x1<-read.csv("Noble2.csv",header=TRUE, stringsAsFactors=FALSE) # `stringsAsFactors` converts time variable to character before conversion. 
 str(x1)
'data.frame':   7633 obs. of  2 variables:
 $ Time: chr  "08/20/14 02:05:02 PM" "08/20/14 02:35:02 PM" "08/20/14 03:05:02 PM" "08/20/14 03:35:02 PM" ...
 $ Temp: num  17 16.7 17 16.8 17 ..

kk<-strptime(x1$Time, "%m/%d/%y %I:%M:%S %p")
> str(kk)
 POSIXlt[1:7633], format: "2014-08-20 14:05:02" "2014-08-20 14:35:02" "2014-08-20 15:05:02" "2014-08-20 15:35:02" ...