Navin
Navin

Reputation: 105

R construct datetime from day-of-year, origin and minutes

I have a big csv file where the first column is the day of the year and the second column is the time in minutes (ie. 00:00): year of measurement=2010

1,0,2.701,104.3,5.02,2.985,13.71,645.97,1018.9,6.249,4.166,.501 
1,1,2.701,104.3,5.02,2.985,13.71,645.97,1018.9,6.249,4.166,.501 
1,2,2.737,104.3,5.228,3.006,13.71,645.97,1018.9,6.249,4.166,.518
1,3,2.805,104.3,4.958,3.027,13.71,645.97,1018.8,7.08,3.749,.767
1,4,2.821,104.3,4.999,3.006,13.71,645.97,1018.9,7.08,4.166,.79
1,5,2.847,104.2,5.208,3.048,13.72,645.97,1018.9,6.249,4.166,.567
1,6,2.881,104.2,4.853,3.027,13.71,645.97,1018.7,6.666,4.166,.688

I want to merge column 1 and 2 using datetime classes in R such that I get one single column starting with 2010-01-01 00:00:00

Cheers, Navin

Upvotes: 2

Views: 449

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162371

From the example data you've supplied, the general format of your second column is not obvious. In particular, how are times that do not fall in the first hour of the day (e.g. 1:01 AM) represented?

Depending on that second column's format, you should be able to cobble together something like the following:

d <- data.frame(day=1, minute=1:10)

s <- paste(2010, d$day, d$minute, sep="-")
strptime(s, format="%Y-%j-%M")
#  [1] "2010-01-01 00:01:00" "2010-01-01 00:02:00" "2010-01-01 00:03:00"
#  [4] "2010-01-01 00:04:00" "2010-01-01 00:05:00" "2010-01-01 00:06:00"
#  [7] "2010-01-01 00:07:00" "2010-01-01 00:08:00" "2010-01-01 00:09:00"
# [10] "2010-01-01 00:10:00"

Here, %j represents "day of year as decimal number (001-366)", as documented in ?strptime. (See that same help file for descriptions of the many other time formats that support conversion to and from R's POSIXt date-time classes).

Upvotes: 5

Related Questions