0

The data frame I am working with:

head(ossub3)

date---------|os5 | os6 |  os7| os8 | _ _Year| Month|Day|Hour|Minutes 
11/15/2015|10.06|16.94|10.63|12.94  |2015|11|15| 12|08 

I have all of the time units separated into columns. I would like one datetime column in R. I have tried the solutions on R tick data : merging date and time into a single object and on Strip the date and keep the time because when I try to

#Combine Date 

ossub3$Date<-as.POSIXlt(paste(ossub3$Year, ossub3$Month, ossub3$Day, sep="-"), 
    tz = "US/Central", format = "%Y-%m-%d")

#Create new column called Time which concatenates Hour, Minute as a POSIXlt 

ossub3$sec=rep(00,nrow(ossub3))
ossub3$Time<-as.POSIXlt(paste(ossub3$Hour, ossub3$Minute,ossub3$sec, sep=":"), 
    tz = "US/Central", format = "%H:%M")

I get today's date in my time so I can't combine it with the date. What is going on here?

Community
  • 1
  • 1
werner2
  • 1
  • 2

1 Answers1

1

One last step is needed:

ossub3$sec=rep(00,nrow(ossub3))
ossub3$Time<-as.POSIXlt(paste(ossub3$Hour, ossub3$Minute,ossub3$sec, sep=":"), tz = "US/Central", format = "%H:%M")

# Excluding the date information and preserving just the time
ossub3$JustTime <- sapply(ossub3$Time, function(x) {format(x, format = "%T")})

By doing so, you are removing the date information (which is the current date and meaningless in your case), and you can attach just the time to the date you have in the other column (ossub3$Date).

Matin Kh
  • 5,192
  • 6
  • 53
  • 77
  • Hey Matin-I could be completely incompetent but I enter in the code as written and I got an Error. Specifically, Error during wrapup: invalid 'trim' argument. Do I need to make "x" anything or is there something I am missing? – werner2 Jul 29 '16 at 18:19
  • It should have been explicitly mentioned `format = "%T"`. Check the answer now. – Matin Kh Jul 29 '16 at 18:29
  • I wonder if you checked the updated answer. If this is the solution, please mark my response as the answer to your question. – Matin Kh Jul 31 '16 at 21:52
  • Sorry my wife had a baby and I haven't looked at it in awhile – werner2 Aug 31 '16 at 19:13