Convert string to timestamps in R - 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")

Related

Input string too long error if using strptime in R

My file contains a list of timestamps:
Fri Feb 14 19:07:31 +0000 2014
Fri Feb 14 19:07:46 +0000 2014
Fri Feb 14 19:07:50 +0000 2014
Fri Feb 14 19:08:04 +0000 2014
and reading it into R using:
dataset <- read.csv(file="Data.csv")
and i then write R commands to enable R to detect the timestamps:
time <- strptime(dataset,format = "%a %b %d %H:%M:%S %z %Y", tz = "GMT")
but I'm constantly getting an error saying:
Error in strptime(dataset, format = "%a %b %d %H:%M:%S %z %Y") :
input string is too long
it was working well at first but after i added:
defaults write org.R-project.R force.LANG en_US.UTF-8
in my terminal to fix some preferences in R for mac os x, the timestamp command stopped working an keep producing the error i mentioned above.
This is your original Data. myDates as Character.
dtData<-data.frame(myDates=c( "Fri Feb 14 19:07:31 +0000 2014",
"Fri Feb 14 19:07:46 +0000 2014",
"Fri Feb 14 19:07:50 +0000 2014",
"Fri Feb 14 19:08:04 +0000 2014"))
> dtData
myDates
1 Fri Feb 14 19:07:31 +0000 2014
2 Fri Feb 14 19:07:46 +0000 2014
3 Fri Feb 14 19:07:50 +0000 2014
4 Fri Feb 14 19:08:04 +0000 2014
you need to select dtData$myDates column
time <- strptime(dtData$myDates,format = "%a %b %d %H:%M:%S %z %Y", tz = "GMT");time
[1] "2014-02-14 19:07:31 GMT" "2014-02-14 19:07:46 GMT"
[3] "2014-02-14 19:07:50 GMT" "2014-02-14 19:08:04 GMT"

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"

Chr to date in 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.

Resources