Chr to date in R - r

How can I convert a chr value such as: "Thu Jul 24 11:58:12 CEST 2014" to Date format in R?
I've tried:
dataset$acquiredDateTime = strptime(dataset$acquiredDateTime, "%a %b %d %H:%M:%S %Y")
but it returns:
acquiredDateTime : POSIXt, format NA NA NA NA ...
Even as. Date:
dataset$acquiredDateTime = as.Date(dataset$acquiredDateTime, format="%m/%d/%Y %H:%M:%S")
is not working. It returns:
acquiredDateTime : Date, format NA NA NA NA ...

You need to include the CEST string in your template, or process it out:
x="Thu Jul 24 11:58:12 CEST 2014"
strptime(x, "%a %b %d %H:%M:%S CEST %Y")
[1] "2014-07-24 11:58:12 UTC"
I don't have experience with interpreting timezones for input.

You could try:
v1 <- "Thu Jul 24 11:58:12 CEST 2014"
strptime(gsub("CEST", "", v1), "%a %b %d %H:%M:%S %Y", tz="CEST")
#[1] "2014-07-24 11:58:12 CEST"

This worked perfectly fine for me.
strptime("Thu Jul 24 11:58:12 CEST 2014", "%a %b %d %H:%M:%S")
[1] "2014-07-24 11:58:12 IST"
EDIT 1: Ok, I just saw the "CEST" part. Let me update the answer in a bit.
EDIT 2: Other answers have covered it.

Related

Convert string to timestamps in R

I have the string which is formatted as below:
Tue Feb 11 12:28:36 +0000 2014
I try to convert this string to timestamps in R by using:
timeobj <- strptime(df[1], format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT")
where df[1] is in format of Tue Feb 11 12:28:36 +0000 2014
However, I got an error as below:
Error in strptime(df[1], format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT") :
input string is too long
How can I fix this?
dput(df[ 1:5, 1]) =
c("Tue Feb 11 12:47:26 +0000 2014", "Tue Feb 11 12:55:09 +0000 2014", "Tue Feb 11 13:22:29 +0000 2014", "Tue Feb 11 13:24:31 +0000 2014", "Tue Feb 11 13:34:00 +0000 2014")
It looks like that your locale is not fitting the abbreviated weekday and month name.
x <- c("Tue Feb 11 12:47:26 +0000 2014",
"Tue Feb 11 12:55:09 +0000 2014", "Tue Feb 11 13:22:29 +0000 2014",
"Tue Feb 11 13:24:31 +0000 2014", "Tue Feb 11 13:34:00 +0000 2014")
Sys.setlocale("LC_ALL", "de_AT.UTF-8")
strptime(x, format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT")
#[1] NA NA NA NA NA
Sys.setlocale("LC_ALL", "C")
strptime(x, format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT")
#[1] "2014-02-11 12:47:26 GMT" "2014-02-11 12:55:09 GMT"
#[3] "2014-02-11 13:22:29 GMT" "2014-02-11 13:24:31 GMT"
#[5] "2014-02-11 13:34:00 GMT"
The manual of strptime says: '%a' Abbreviated weekday name in the current locale on this platform.
Also it looks like you are providing a data.frame with df[1] and not a vector which can probably provided with df[,1].
%T is enough for the time.
timeobj <- strptime(df[1], format = "%a %b %e %T %z %Y", tz = "GMT")

Why is as.POSIXct outputting BST instead of UTC?

as.POSIXct("Tue Aug 18 2020 08:45:02 GMT+0000 (Coordinated Universal Time)", tx="UTC", format = "%a %b %d %Y %H:%M:%OS")
[1] "2020-07-13 08:24:03 BST"
same output with GMT instead of UTC set as the timezone
as.POSIXct("Tue Aug 18 2020 08:45:02 GMT+0000 (Coordinated Universal Time)", tx="GMT", format = "%a %b %d %Y %H:%M:%OS")
[1] "2020-07-13 08:24:03 BST"
for reference I'm doing this in Rstudio
The option for timezone is "tz" not "tx". This works as expected:
> as.POSIXct("Tue Aug 18 2020 08:45:02 GMT+0000 (Coordinated Universal Time)", tz="UTC", format = "%a %b %d %Y %H:%M:%OS")
[1] "2020-08-18 08:45:02 UTC"

Convert timestamp to a date-time format in R

I have a dataset, with a column with automatically generated timestamps in the format.
head(tweets$V2)
[1] Fri Oct 30 18:33:50 +0000 2015 Fri Oct 30 18:33:51 +0000 2015 Fri Oct 30 18:33:52 +0000 2015
[4] Fri Oct 30 18:33:54 +0000 2015 Fri Oct 30 18:33:55 +0000 2015 Fri Oct 30 18:33:56 +0000 2015
I want to convert these to a POSIX type time-date format. Any pointers on how do I go about with this?
After converting these to a standard time format, I wanted to observe trends in the subjects of the tweets.
See ?strptime for more details:
tweets$V2 <- as.POSIXct(strptime(tweets$V2, "%a %b %d %H:%M:%S %z %Y"))
This will convert the strigs in POSIXct format with the default time zone of your system. If you want to specify a different timezone, include the tz argument.
a <- c("Fri Oct 30 18:33:50 +0000 2015")
as.POSIXct(strptime(a, "%a %b %d %H:%M:%S %z %Y"))
[1] "2015-10-30 14:33:50 EDT"
as.POSIXct(strptime(a, "%a %b %d %H:%M:%S %z %Y", tz = "GMT"))
[1] "2015-10-30 18:33:50 GMT"
Note: Convert the column with as.character if it is of class factor

How to display date in month and year format

I have a date field called date.timestamp in database,which has date values like "Fri Nov 27 20:17:01 IST 2015" .There are lot of records having date.timestamp field. I need to display it as Nov 2015 for all those records in database. How can I do that?
Assuming IST is the Indian Standard Time, which is 5:30 hours ahead of Coordinated Universal Time, one would substitute IST for +0530 and use the format %z of strptime.
vec <- "Fri Nov 27 20:17:01 IST 2015"
format(strptime(sub("IST", "+0530", vec), "%a %b %d %H:%M:%S %z %Y"), "%b %Y")
# [1] "Nov 2015"
vec <- c("Fri Nov 27 20:17:01 IST 2015","Mon Nov 30 20:17:01 IST 2015")
format(strptime(sub("IST", "+0530", vec), "%a %b %d %H:%M:%S %z %Y"), "%b %Y")
# [1] "Nov 2015" "Nov 2015"

how to convert a string like "Sat Mar 17 11:27:57 +0000 2012" into date in R

The question is like the title:
The date date i got is in the format like "Sat Mar 17 11:27:57 +0000 2012".
How could i convert it into R's date data?
You just need to specify the correct format (as documented in strptime):
fmt <- "%a %b %d %H:%M:%S %z %Y"
# POSIXct
as.POSIXct("Sat Mar 17 11:27:57 +0000 2012", format=fmt, tz="UTC")
[1] "2012-03-17 11:27:57 UTC"
# POSIXlt
strptime("Sat Mar 17 11:27:57 +0000 2012", format=fmt, tz="UTC")
[1] "2012-03-17 16:27:57 UTC"

Resources