Nata
Nata

Reputation: 171

How to convert to unix time in r?

I want to convert this date format to unix timestamp.I need this to find difference between two columns of dates.

Fri Apr 01 04:32:50 +0000 2011

What is the better way to do it in R? Should I remove first symbols that represent weekday and than use as.POSIXlt()?

Upvotes: 2

Views: 1216

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

Just use strptime and cast to numeric:

ts <- strptime("Fri Apr 01 04:32:50 +0000 2011",  "%a %b %d %H:%M:%S %z %Y")
as.numeric(ts)

[1] 1301632370

Demo

Upvotes: 3

Related Questions