1

I have one xts object, and another data frame with 3 columns (YY, MM, DD). I am trying to create / convert these three columns to some sort of date/time format so that I can merge it with an xts tables together. No luck at all.

Columns YY,MM,D are the years, months and days in numeric..

Tried something along these lines, but couldn't get it working.

t.date<-cbind(YY,MM,DD)
as.POSIXct(t.date)
davepmiller
  • 2,620
  • 3
  • 33
  • 61
simon
  • 139
  • 1
  • 1
  • 5

1 Answers1

2

A reproducible example:

df <- data.frame( YY = c(12,12,12,13,12) , MM = c(01,01,01,01,02) , DD = c(10,10,10,10,10) )
#  YY MM DD
#1 12  1 10
#2 12  1 10
#3 12  1 10
#4 13  1 10
#5 12  2 10

dates <- paste(df$YY,df$MM,df$DD , collapse=NULL , sep = "/" )
# [1] "12/1/10" "12/1/10" "12/1/10" "13/1/10" "12/2/10"

t.date <- strptime( dates , format = "%y/%m/%d")
# [1] "2012-01-10" "2012-01-10" "2012-01-10" "2013-01-10" "2012-02-10"
class(t.date)
# [1] "POSIXlt" "POSIXt" 

Or for POSIXct:

as.POSIXct( dates , format = "%y/%m/%d" , tz = "GMT") 
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184