0

I want to turn the column x$Timestamp into time R can read using as.POSIXct and the new readable time in x$Timestamp. I am looking to use apply or a for loop. Any ideas?

code:

x;
    Timestamp Barcode
1   2014-01-11 2012576
2   2014-01-11 2012458
3   2014-01-11 2012463
4   2014-01-11 2012421
5   2014-01-11 2012457
6   2014-01-11 2012416
7   2014-01-11 2012468
8   2014-01-11 2012462
9   2014-01-11 2012452
10  2014-01-11 2012511
11  2014-01-11 2012437
12  2014-01-11 2012406
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Chad
  • 149
  • 4
  • 11

1 Answers1

1

Don't use a for loop, this is R! You can simply do this:

x$pTimestamp <- as.POSIXct(x$Timestamp)
weitzner
  • 440
  • 3
  • 12
  • @ weitzner, I should have been more clear, I can do the above, I want to do that but also keep the barcode associate with the time stamp. Sorry I am new, please forgive. cheers chad – Chad Jan 24 '14 at 21:13
  • The above line will just add a third column to your data.frame. The converted data will be in the same order as the initial column, so the barcodes will still be in sync with the new column. – weitzner Jan 24 '14 at 21:35
  • @ could I just do this x[,1] <- as.POSIXct(x[,1]), and change class since date is already in correct form, but wrong class? – Chad Jan 24 '14 at 21:48
  • Yes. You could even use the column title, which is considered more readable and stylish, like so:`x$Timestamp <- as.POSIXct(x$Timestamp)` – weitzner Jan 24 '14 at 21:55
  • @ thanks alot weitzner, you were a big help. Thanks for the quick lesson. cheers – Chad Jan 24 '14 at 22:01