John Smith
John Smith

Reputation: 2886

R: Convert a Character to a timestamp

Currently we are implementing some sort of direct marketing We send an SMS (timestamp) on a certain day from a certain country within Europe and then receive a response from a customer in the local country if they are interested in the product

I have a sample data set for two months, of SMS's sent and Responses received (if at all). The end goal is , I want to calculate the time difference in hours between the sent time and the response time factoring in Time Zones and Day Light Savings

The file i received has the time stamps in character format. I wish to convert them to dates of their local timezones, convert them to a standard zone and calculate the hours difference

I have tried the methods below to just create the conversion but it comes back with NA. Any help would be greatly appreciated

as.POSIXlt("12/02/2015 11:23", tz = "Europe/London")
strptime("12/02/2015 11:23", "%d/%m/%Y %h:%m")
as.Date("12/02/2015 11:23","%d/%m/%y %h:%m")

Upvotes: 1

Views: 11141

Answers (2)

Uma Ranjith
Uma Ranjith

Reputation: 1

$ started_at : chr "1/21/23 20:05" "1/10/23 15:37" "1/2/23 7:51" ...

year_2023 is a data set and it has the field called started_at is as character , to convert the character data type to timestamp type follow these steps:

1.check the month, date, year order and the hour, minutes, seconds pattern. 2.use / if the values is separated by /(forward slash) or - for - (hyphen) between values

year_2023$date <- strptime(year_2023$started_at, "%m/%d/%y %H:%M")

this won't bring back any NA

Upvotes: 0

tchakravarty
tchakravarty

Reputation: 10984

Note that you do not say if you are using 24-hour or 12-hour clock, but given that you do not have AM/PM, I am going to assume the former:

strptime("12/02/2015 11:23 AM", "%d/%m/%Y %I:%M %p", tz = "Europe/London")  #12-hour
strptime("12/02/2015 11:23", "%d/%m/%Y %H:%M", tz = "Europe/London")  # 24-hour

Upvotes: 5

Related Questions