0

I am a beginner R user and would need to do something that is wrecking my head. I have 3 csv files, each with 2 columns: 1 for time (in the for of "month/day/year hour:minute:second am/pm") and 1 for temperatures. The times of the measurements are different for the 3 files. The first problem I get and I don't seem to overcome is that when opening the csv file, R does not recognise the "Time" column as time (it attributes the class of "character").

This is how the file looks like (the file's name is 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

and these are the commands:

class(Noble2$Time) [1] "character"

the csv file is here: https://www.dropbox.com/s/9lljwadmnp9uvot/Noble2.csv?dl=0

Thank you very much for your time!

1
  • 2
    You should at least create a little example and post it as part of your question. It is too hard to help you otherwise.
    – Mike Wise
    Feb 15, 2015 at 16:29

1 Answer 1

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" ...
10
  • I used: strptime(Noble2$Time, "%m/%d/%y %I:%M:%S %p"); %I to state it's 0-12 h and %p to state the AM/PM nature of the time. However, the class did not change: > class(Noble2$Time) [1] "character" Feb 15, 2015 at 20:35
  • can you dput the data frame as dput(Noble) for your sample data and add in the question?
    – Metrics
    Feb 15, 2015 at 20:41
  • couldn't find out how to add it that way, Metrics. Added a dropbox link... Is it good enough? Sorry and thank you for your help Feb 15, 2015 at 20:52
  • Thank you very much, Metrics. I'll have to "investigate" your answer to get to my final goal of plotting 3 files like that one. But, if I am understanding it right, the times will no longer be related with the temperatures in a data.frame, right? Feb 15, 2015 at 21:37
  • Sorry, I am new to this. Thank you although I am sure tomorrow I'll have another question to add to this ;) Feb 15, 2015 at 21:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.