I'm trying to convert characters into a POSIXct format using lubridate's parse date_time function.
This is my code:
df$TimeClosed <- parse_date_time(df$TimeClosed, 'mdy HMSp')
Most of my variables are in this format : 09/12/2017 11:08:51 AM
but there are some that are like this : 7/6/19 15:37
and because of that, they are failing to parse.
Any suggestions on how I can fix this so all date/times are in POSIX and in the same format?
Thanks,
parse_date_time can take a vector of formats and will try them one by one in order until one matches.
library (lubridate)
x <- c("09/12/2017 11:08:51 AM", "7/6/19 15:37")
parse_date_time(x, c("dmYHMSp", "dmyHM"))
## [1] "2017-12-09 11:08:51 UTC" "2019-06-07 15:37:00 UTC"
Related
I have data "A" in the format chr "5/7/2021 15:15". I would like to convert it to a format which R will recognize. (It is giving me errors when I try to plot, for instance, which leads me to believe it needs to be reformatted.)
Here is the format "B" I would like to achieve. R seems to like this ok, so I might as well match it (?):
POSIXct, format: "2021-8-11 16:00:00". I am not sure if the seconds are needed, and they do not exist in data "A" so the seconds could be omitted. If R doesn't care then I don't either. The timezone is UTC.
How do I do it? I have tried a couple things, including:
CTD_datetime_UTC <- as.POSIXct(CTD$Date.and.Time, tz = "UTC").
You can use strptime from base R. But there are many parsers for dates...
Assuming the format is "day/month/year" (example is not unambiguous, could also be "month/day/year")
strptime("5/7/2021 15:15", "%d/%m/%Y %H:%M", tz = "UTC")
Returns:
[1] "2021-07-05 15:15:00 UTC"
Using parsedate
library(parsedate)
parse_date("5/7/2021 15:15")
[1] "2021-05-07 15:15:00 UTC"
Is there a way to convert a date character column (with Chinese characters) to a datetime format column in a table in R? The column is now in the form of "xxx年xxx月xxx日". I am a bit new to R and I know that "as.date" could only read certain formats of date characters like xxx-xxx-xxx (xxx are numerical values), but failed to recognize the format in my case.
Basically the data looks like this:
dput(head(lands_full$contract_signed_date))
c("2004年10月11日", "2008年09月10日", "2011年10月25日",
"2011年12月31日", "2018年08月07日", "2016年06月24日"
)
Any help would be appreciated!
This can be done with the package lubridate:
lubridate::parse_date_time(x, '%Y年%m月%d日')
# [1] "2004-10-11 UTC" "2008-09-10 UTC" "2011-10-25 UTC" "2011-12-31 UTC" "2018-08-07 UTC"
# [6] "2016-06-24 UTC"
Timezone can be set with tz argument to parse_date_time.
use
strptime(gsub("\\D","-",x),"%F")
I have the following datetime string:
mytimestr <- "2014-04-15T14:40:00.000000000+00:00"
I assumed it was an ISO3339 format, but the following format string, which should describe that:
> as.POSIXct(mytimestr, format="%Y-%m-%dT%H:%M:%E9S%Ez")
[1] NA
Produces NA as you can see. Questions are either:
Can you identify a format that decodes this time? Or
Is there a common resource or function that can look at a string time and tell me the time format?
We could use ymd_hms from lubridate
library(lubridate)
ymd_hms(mytimestr)
#[1] "2014-04-15 14:40:00 UTC"
I have character field in the following format
df
sd
10/12/2017 6:12
10/12/2017 6:14
I want to convert it into date format so that I can extract time from it.
Now having read this, I don't want to use regex as I want to keep it more generic as the formats might change. So I wanted to convert it to a date format and use lubridate to extract required fields.
So i used the following :
d1 <- strptime(df$sd[1], "%m/%d/%Y%H:%M")
and it gives me the following result
d1
[1] "2017-10-12 IST"
whereas i was expecting the hours and mins to be included as well.
Also on trying to use
format(dmy_hms(df$sd), "%H:%M:%S")
I get that "All formats failed to parse. No formats found"
Any suggestions on this?
Here's the lubridate solution:
library(lubridate)
df <- data.frame(sd=c('10/12/2017 6:12','10/12/2017 6:14'),stringsAsFactors = F)
dmy_hm(df$sd)
#[1] "2017-12-10 06:12:00 UTC" "2017-12-10 06:14:00 UTC"
I need to convert a time in character "01:15:55 AM" to time.
I was thinking of using chron library, however I am not sure how to do the format.
chron(times = "01:15:55 AM", format = "%I:%M:%S %p")
Error in parse.format(format) : unrecognized format %I:%M:%S %p
Any ideas? I'm happy to try other libraries
One option is to use strptime to parse, and then format the result so you can make it into a nice chron::times object:
library(chron)
times(format(strptime("01:15:55 PM", format = "%I:%M:%S %p"), "%H:%M:%S"))
# [1] 13:15:55
testtime<-("2013-07-21 02:00:01 PM")
library(lubridate)
ymd_hms(testtime)
[1] "2013-07-21 14:00:01 UTC"
The canonical way of dealing with date and time objects in R is using the POSIX functions, such as:
> (time.POSIX<-strptime("01:15:55 AM", format = "%I:%M:%S %p"))
[1] "2016-03-31 01:15:55 BRT"
> class(time.POSIX)
[1] "POSIXlt" "POSIXt"
The strptime function, along with all others that work with POSIX dates, such as format (for printing) accept the format string that you're using.
The problem here is that this data is not a time per se, but a date and a time, and the date part is implicitly created as "today". So you can create this time objects in a fixed reference date, say, 2000-01-01.
On the other hand, if you want to use the chron package... they are not compliant with these format specifications. chron manual only specifies m, d, y, h, m and s.
You might have to write a wrapper that understands AM/PM and fixes it.