2

I have a text file that I import into R with read.files:

DataFolder <- "unzipped"
LakeNames <- "Location1"
extension <- "txt"
# -- import data
data <- read.table(paste(DataFolder, LakeNames, paste(LakeNames, ".", extension, sep=""), sep="\\"),
                   header = TRUE, sep = "\t")  

This reads in the data as

> head(data)
          dateTime dat
1 2009-03-01 00:00  0
2 2009-03-01 01:00  0
3 2009-03-01 02:00  0
4 2009-03-01 03:00  0
5 2009-03-01 04:00  0
6 2009-03-01 05:00  0

where

> class(data$dateTime)
[1] "factor"

when I try to convert the dateTime into POSIXct

> data$dateTime <- as.POSIXct(data$dateTime)
> head(data)
    dateTime dat
1 2009-03-01  0
2 2009-03-01  0
3 2009-03-01  0
4 2009-03-01  0
5 2009-03-01  0
6 2009-03-01  0

Why is this not showing the HH:MM part of the dateTime when I convert to POSIXct?

Lucas
  • 3,376
  • 6
  • 31
  • 46
Emma
  • 618
  • 12
  • 26

2 Answers2

3

I posted a related question a few days ago: Is there a specific way to handle timestamp columns in R when pulling data using RPostgreSQL?

What time zone are you on?

Try running

Sys.setenv(TZ='UTC') 

before reading the file.

I wasn't able to see HH:MM data on the POSIXct data. It turns out that if the datetime values in the data include a point in time when clocks move forward by 1 hour, the datetime value with hour 02:00 is invalid, since it's supposed to be skipped. POSIXct converts it to an absolute date with no HH:MM data. Since all the data within a vector needs to have the same format, all the data within the vector also loose the HH:MM part. I hope that helps.

Community
  • 1
  • 1
JAponte
  • 1,508
  • 1
  • 13
  • 21
0

read.table will use the as.POSIXct function if you specify colClasses:

data <- read.table(paste(DataFolder, LakeNames, 
                    paste(LakeNames, ".", extension, sep=""), sep="\\"),
         header = TRUE, sep = "\t", colClasses=c("POSIXct","numeric")) 

It might already be a POSIXct object and is only being displayed with its dates. Try:

format(data$dateTime, "%Y-%m-%d %H:%M:%S")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • The first solution creates the final example shown in the orignal post then by using 'format' it converts the class of data$dateTime to "character", I require POSIXct. – Emma Jul 20 '13 at 20:02
  • The problem is that data$dateTime will have the correct number of dates, but the HH:MM will not show i.e. it only shows the y-m-d – Emma Jul 20 '13 at 20:04
  • I was just trying to show that the data was in there. I wasn't saying you should assign that value to the column. You should say what you are having difficulty doing with the data. If the time data is there then any arithmetic or comparison operations should give correct results. – IRTFM Jul 20 '13 at 20:04
  • OK, but my next task is to calculate the hourly averages which I can do but I require the dates to show the hours, which at the moment they do not. How can solve this? i.e how can I get the data frame to show the full dates and be POSIXct – Emma Jul 20 '13 at 20:07
  • 1
    Somehow by typing Sys.setenv(TZ="GMT") beforehand solved the problem – Emma Jul 20 '13 at 20:12
  • Interesting. I tried setting my tz to an invalid string and still got the usual data-time output. – IRTFM Jul 20 '13 at 20:18