I am looking to convert this character vector to a date format
I have tried various methods though have not been successful so far. Any assistance with this is greatly appreciated!
See ?strptime for format details
as.Date(x, "%d%b%Y:%H:%M:%S")
#[1] "2016-01-11"
Or if you want it in date-time format
as.POSIXct(x, format = "%d%b%Y:%H:%M:%S")
#[1] "2016-01-11 GMT"
With lubridate
library(lubridate)
dmy_hms(x)
#[1] "2016-01-11 UTC"
and if you want only date
ymd(dmy_hms(x))
#[1] "2016-01-11"
data
x <- "11JAN2016:00:00:00.000"
One option is anydate from anytime
library(anytime)
anydate(data)
#[1] "2016-01-11"
data
data <- "11JAN2016:00:00:00.000"
Related
I have a POSIXct variable in the form of "2000-01-01 01:01:01" (ymd hms) but I'd like to drop the ymd so I'm left with only the time (i.e. "01:01:01").
We can use as.hms from
library(hms)
as.hms(as.POSIXct(str1))
#01:01:01
If we need only the string version, format would work as well
format(as.POSIXct(str1), "%H:%M:%S")
#[1] "01:01:01"
data
str1 <- "2000-01-01 01:01:01"
We can also use chron library:
chron::times(strftime(as.POSIXct("2000-01-01 01:01:01"),"%H:%M:%S"))
# [1] 01:01:01
I have searched but I could not find out how to convert a date from a character string formatted as follows:
date <- "07-21-2015-09:30AM"
I wanted to use as.Date, but I have not manage to. All I get is the following:
as.Date(date, format="%m-%d-%y-%hAM")
NA
as.Date(dates, format="%m-%d-%y-%h")
NA
If we need the 'date' and 'time', one option is as.POSIXct
as.POSIXct(date, format='%m-%d-%Y-%I:%M%p')
#[1] "2015-07-21 09:30:00 EDT"
You can also use the lubridate package like this:
library('lubridate')
date <- "07-21-2015-09:30AM"
mdy_hm(date)
# "2015-07-21 09:30:00 UTC"
I like strptime for this:
strptime(date, format="%m-%d-%Y-%R%p")
#[1] "2015-07-21 09:30:00 EDT"
And in the case that you needed to see the date in the same format as entered, you can call the related strftime. It doesn't change the internal storage of the variable, rather it changes the format only.
strftime(xx, format="%m-%d-%Y-%R%p")
#[1] "07-21-2015-09:30AM"
I have searched but I could not find out how to convert a date from a character string formatted as follows:
date <- "07-21-2015-09:30AM"
I wanted to use as.Date, but I have not manage to. All I get is the following:
as.Date(date, format="%m-%d-%y-%hAM")
NA
as.Date(dates, format="%m-%d-%y-%h")
NA
If we need the 'date' and 'time', one option is as.POSIXct
as.POSIXct(date, format='%m-%d-%Y-%I:%M%p')
#[1] "2015-07-21 09:30:00 EDT"
You can also use the lubridate package like this:
library('lubridate')
date <- "07-21-2015-09:30AM"
mdy_hm(date)
# "2015-07-21 09:30:00 UTC"
I like strptime for this:
strptime(date, format="%m-%d-%Y-%R%p")
#[1] "2015-07-21 09:30:00 EDT"
And in the case that you needed to see the date in the same format as entered, you can call the related strftime. It doesn't change the internal storage of the variable, rather it changes the format only.
strftime(xx, format="%m-%d-%Y-%R%p")
#[1] "07-21-2015-09:30AM"
I would like to know what is the best way to convert 201509122150 (numeric) to Date class within YYYY-MM-DD hh:mm format.
E.g.
x <- 201509122150
as.POSIXct(as.character(x), format="%Y%m%d%H%M")
# [1] "2015-09-12 21:50:00 CEST"
This can be easily done with lubridate
library(lubridate)
ymd_hm(x)
#[1] "2015-09-12 21:50:00 UTC"
data
x <- 201509122150
My date information is a string in the following format: 3/12/1956 0:00:00
I have tried converting it using DOB<-as.Date(DOB, "%d/%m/%y %H:%M:%S")
I am trying to convert it for the purpose of then applying the age_calc function in eeptools package.
Is there some other way to change a non standard format into a date. Damn Aussie dates!
You can try lubridate as an alternative
library(lubridate)
DOB <- '3/12/1956 0:00:00'
mdy_hms(DOB)
#[1] "1956-03-12 UTC"
It can also take multiple formats
DOB <- c('3/12/1956 0:00:00', '3.12/1956 0.00/00')
mdy_hms(DOB)
#[1] "1956-03-12 UTC" "1956-03-12 UTC"
Or as #Richard Scriven commented,
as.Date(DOB, "%d/%m/%Y %H:%M:%S")
#[1] "1956-12-03"