R: convert character vector to POSIXlt - r

I'm wondering how to convert a column in a data frame that contains character vectors like this one "Mon Aug 19 05:00:07 +0000 2013" into the POSIXlt format.
df$created_at<-as.POSIXlt(df$created_at, format= "%a %b %d %H:%M:%S %z %Y",tz="")
gives me NA's
I followed http://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html
Thank you!

The problem seems to be locale-related. %a and %b match abbreviated day and month name in the current locale, so if your current locale is not set to english, the Mon and Aug in your example won't be recognised as day and month names.
One workaround can be to set your locale to english or to C, also known as the POSIX locale. This can be done with :
Sys.setlocale("LC_TIME", "C")

Related

How to change a column of characters into date form?

I currently have data as such:
data
I wish to change the 'date' column to date type. (it is now in character).
I have tried the code below but it gives me 'NA' as the result.
as.Date(data$Date, format = "%a %d-%m-%Y %I:%M %p")
Am I making a mistake in the way I am formatting the date to suit the format in my data?
Unfortunately, we don't have your data (we have a picture of your data, but the only way to use that is to transcribe it manually - best to include data as text in your questions).
Anyway, using a brief example in the same format:
dates <- c("Wed 25-Apr-2018 3:20 PM", "Thu 10-Mar-2022 10:53 AM")
We can get the dates by doing:
as.Date(dates, "%a %d-%b-%Y %I:%M %p")
#> [1] "2018-04-25" "2022-03-10"
Note though that this does not preserve the time, and to do this you probably want to use R's built in date-time format, POSIXct. We can get this with:
strptime(dates, "%a %d-%b-%Y %I:%M %p")
#> [1] "2018-04-25 15:20:00 BST" "2022-03-10 10:53:00 GMT"
I think the main problem was that you were using %m for the month, but this only parses months in decimal number format. You need %b for text-abbreviations of months.

Can format like "May 17, 2017", "17/5/2017" or "17-5-17 05:24:39" be used in as.POSIXlt?

I've just read about the difference between POSIXlt and POSIXct and it is said that POSIXlt is a mixed text and character format like "May, 6 1985", "1990-9-1" or "1/20/2012". When I try such kind of things I get an error
as.POSIXlt("May, 6 1985")
# character string is not in a standard unambiguous format
(How) can we dates with format as quoted above put forward to POSIXlt? Here are sources saying that such format works (if I get them right): 1, 2.
Read ?strptime for all the details of specifying a time format. In this case you want %b (for month name), %d (day of month), %Y (4-digit year). (This will only work with an English locale setting as the month names are locale-specific.)
as.POSIXlt("May, 6 1985", format = "%b, %d %Y")
If you have mixed input of formates you can use parse_date_time from the lubridate package.
x <- c("May, 6 1985", "1990-9-1", "1/20/2012")
y <- lubridate::parse_date_time(x, c("ymd", "mdy", "dmy"))
str(y)
# POSIXct[1:3], format: "1985-05-06" "1990-09-01" "2012-01-20"
note on c("ymd", "mdy", "dmy") as this determines the order on first found first converted. Consider 6-1-2000 will encounter mdy as valid before dmy so this means it will be first of June and not sixth of January.

How can as.Date() convert fully written dates into ISO 8601? [duplicate]

This question already has an answer here:
Format for ordinal dates (day of month with suffixes -st, -nd, -rd, -th)
(1 answer)
Closed 1 year ago.
I currently have a vector of dates that are in the following format:
a <- c("Wednesday 26th May 2021","Thursday 27th May 2021")
I've tried to get it into ISO 8601 using the following:
as.Date(a, "%I %d%S %F %Y")
But I'm not 100% certain about the syntax of writing dates.
Any thoughts are appreciated!
You can remove the date suffixes and use as.Date -
#Added an extra date that does not have th as prefix.
a <- c("Wednesday 26th May 2021","Thursday 27th May 2021",
'Tuesday 1st June 2021', 'Monday 31st May 2021')
as.Date(sub('(?<=\\d)(th|rd|st|nd)', '', a, perl = TRUE), '%A %d %b %Y')
#[1] "2021-05-26" "2021-05-27" "2021-06-01" "2021-05-31"
Read ?strptime for different format specification.
If you are open to packages lubridate::dmy works directly.
lubridate::dmy(a)
#[1] "2021-05-26" "2021-05-27" "2021-06-01" "2021-05-31"

How to convert full date character into mm/dd/yyyy in R?

I have a dataset in R with a date column that is currently being treated as a character column. The dates in this column are listed as "August 24 2012" and I am trying to convert it into a date format such as 08/24/2012.
I am a novice with R and have tried to use format() and lubridate with no success. How do I convert these dates from a character to date?
We need to convert it to Date class and then use format
format(as.Date(dates, "%B %d %Y"), "%m/%d/%Y")
#[1] "08/24/2012"
As the order is month, day, year use mdy from lubridate
library(lubridate)
format(mdy(dates), "%m/%d/%Y")
#[1] "08/24/2012"
It is based on the order, e.g.
ydm("2012 24 August")
#[1] "2012-08-24"
data
dates <- "August 24 2012"

Extracting the hour from a date

I have a date in a semi-odd format. It's written like:
Tue Oct 11 20:56:33 +0000 2016
The date is a string. The date shows what time and day a tweet was sent as in a data set. I want to find what hour each of my tweets were sent out. What is the best way to work with this? Is there a way to convert it to a date type easily, or is there a way to work with that string with what I want to do? Obviously I'd like to something like:
format(strptime(testTweets$Date, format = "%m %d %H:%M%S %Y", "%H"))
But I'm not sure how to work with the day of the week or the +0000. Thanks for the help!
The above comment beat me to it, but try the following code:
strptime(x, format = "%a %b %d %H:%M:%S %z %Y")
[1] "2016-10-11 20:56:33"
You only need to check the documentation for strptime to figure out the correct format mask to use:
https://www.rdocumentation.org/packages/base/versions/3.4.3/topics/strptime

Resources