Dmitry Leykin
Dmitry Leykin

Reputation: 505

converting date into timestamp in R

i've searched for threads about timestamp conversion in R, but could not figure this out. I need to convert time column into timestamp so R would read it as dates. When the cell has only date without time, there is no problem, but the current format (either with + or without it in the cell - R considers it as integer or factor). How do i convert it into timestamp?

thank you enter image description here

Upvotes: 1

Views: 3380

Answers (2)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

You do not need to remove the +:

R> crappyinput <- c("2014-11-29 15:23:02+", "2014-11-29 15:38:36+", 
+                   "2014-11-29 15:52:49+")
R> pt <- strptime(crappyinput, "%Y-%m-%d %H:%M:%S")
R> pt
[1] "2014-11-29 15:23:02 CST" "2014-11-29 15:38:36 CST" "2014-11-29 15:52:49 CST"
R> 

It will simply be ignored as trailing garbage.

Upvotes: 4

MLavoie
MLavoie

Reputation: 9836

would this work for you?

t <- c("2014-11-29 15:23:02+")
t <- substr(t, 1, nchar(t)-1)
t
[1] "2014-11-29 15:23:02"
t <- strptime(t, format="%Y-%m-%d")
str(t)
 POSIXlt[1:1], format: "2014-11-29"

Upvotes: 1

Related Questions