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
Related
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"
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"
Date
01/01/2013 #mm/dd/yyyy
12122015 #mmddyyyy
2014-03-21 #yyyy-mm-dd
95.10.12 #yy.mm.dd
I have a column "Date" with different formats. How can I clean this and convert them into a single date format?
Additional info:class(Date) is factor.
The easiest way to do this is to use the lubridate package.
Date=c(
"01/01/2013" #mm/dd/yyyy
,"12122015" #mmddyyyy
,"2014-03-21" #yyyy-mm-dd
,"95.10.12" #yy.mm.dd
)
library(lubridate)
# list of functions in lubridate that
# translate text to POSIXct: you only need to know the
# order of the data (e.g. mdy = month-day-year).
funs <- c("mdy","dym","ymd","dmy","myd","ydm")
# vector to store results
dates <- as.POSIXct(rep(NA,length(Date)))
# we try everything lubridate has. There will be some warnings
# e.g. because mdy cannot translate everything. You can ignore this.
for ( f in funs ){
dates[is.na(dates)] <- do.call(f,list(Date[is.na(dates)]))
}
dates
> dates
[1] "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET" "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET"
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"