How to go from yyyy-mm-dd to dd-mm-yyyy? - r

The follwing code:
date.seq <- as.Date("2007-10-01"):as.Date("2007-10-31")
new.dates <- data.frame(date = as.Date(date.seq, origin = "1970-01-01"))
gives the following output:
> new.dates
date
1 2007-10-01
2 2007-10-02
3 2007-10-03
4 2007-10-04
5 2007-10-05
I would like to have the dates in dd-mm-yyyy format.
I have tried as.Date(new.dates, "%d/%m/%y") without succes.

a) Parse
d <- as.Date( "06.12.2012", "%d.%m.%Y")
b) Format
strftime(d, "%m-%d-%Y")
or
format(d, "%m-%d-%Y")
so Find the below code.
date.seq <- as.Date("2007-10-01"):as.Date("2007-10-31")
new.dates <- data.frame(date = as.Date(date.seq, origin = "1970-01-01"))
new.dates1 <- as.Date( new.dates$date, "%d.%m.%Y")
strftime(new.dates1, "%m-%d-%Y")

Here's another approach. Just for fun:
sub("^(.*)-(.*-)(.*)$", "\\3-\\2\\1", new.dates[[1]])
# [1] "01-10-2007" "02-10-2007" "03-10-2007" "04-10-2007" "05-10-2007"
# [6] "06-10-2007" "07-10-2007" "08-10-2007" "09-10-2007" "10-10-2007"
# [11] "11-10-2007" "12-10-2007" "13-10-2007" "14-10-2007" "15-10-2007"
# [16] "16-10-2007" "17-10-2007" "18-10-2007" "19-10-2007" "20-10-2007"
# [21] "21-10-2007" "22-10-2007" "23-10-2007" "24-10-2007" "25-10-2007"
# [26] "26-10-2007" "27-10-2007" "28-10-2007" "29-10-2007" "30-10-2007"
# [31] "31-10-2007"

This also works:
new.dates <- data.frame(format(as.Date(1:31, origin = "1970-01-01"),"%d/%m/%Y"))
In your original code, as.Date(start):as.Date(end) creates a vector of integers, which you transform back to date using as.Date(...) a second time.

Related

Use as.Date with tryFormats to parse dates with different formats

I have a variable with dates in a two different formats ("%Y-%m-%d" and "%m/%d/%Y"):
dput(df)
structure(1:8, .Label = c("2019-04-07", "2019-04-08", "2019-04-09",
"2019-04-10", "7/29/2019", "7/30/2019", "7/31/2019", "8/1/2019"
), class = "factor")
# [1] 2019-04-07 2019-04-08 2019-04-09 2019-04-10 7/29/2019 7/30/2019 7/31/2019 8/1/2019
# 8 Levels: 2019-04-07 2019-04-08 2019-04-09 2019-04-10 7/29/2019 7/30/2019 ... 8/1/2019
I try to parse the dates using as.Date with tryFormats
df <- as.character(df)
d <- as.Date(df, tryFormats = c("%Y-%m-%d", "%m/%d/%Y"))
which converts the first format structure, but then returns NA for the second format structure. If I run the two formats separately, they look good though:
t1 <- as.Date(df, format = "%Y-%m-%d")
t2 <- as.Date(df, format = "%m/%d/%Y")
t1
# [1] "2019-04-07" "2019-04-08" "2019-04-09" "2019-04-10" NA
# [6] NA NA NA
t2
# [1] NA NA NA NA "2019-07-29"
# [6] "2019-07-30" "2019-07-31" "2019-08-01"
Any suggestions? I've looked through other responses, but haven't found any good tryFormats examples/questions that seem to address this.
tryFormats will only select one of the given formats. In your case you can convert them individually, as you have already done.
d <- as.Date(df,format="%Y-%m-%d")
d[is.na(d)] <- as.Date(df[is.na(d)],format="%m/%d/%Y")
d
#[1] "2019-04-07" "2019-04-08" "2019-04-09" "2019-04-10" "2019-07-29"
#[6] "2019-07-30" "2019-07-31" "2019-08-01"
We can use anydate from anytime
library(anytime)
anydate(df)
If any of the formats are not present, use addFormats() to add that format and then apply the function
Or with lubridate
library(lubridate)
as.Date(parse_date_time(df, c("ymd", "mdy")))
For base solution, you may try the following as explained in this answer:
> df
#[1] "2019-04-07" "2019-04-08" "2019-04-09" "2019-04-10" "7/29/2019" "7/30/2019"
#"7/31/2019" "8/1/2019"
fmts <- c("%Y-%m-%d","%m/%d/%Y")
as.Date(apply(outer(df, fmts, as.Date),1,na.omit),'1970-01-01')
#[1] "2019-04-07" "2019-04-08" "2019-04-09" "2019-04-10" "2019-07-29" "2019-07-30" "2019-07-31" "2019-08-01"

How to convert character and dates to dates?

My data comes from excel. The dates are in dd/mm/yyyy format:
certificado$fecha <- c("22/02/2019", "43679", "22/02/2019", "22/01/2019", "28/10/2019",
"18/09/2019")
However, R is reading some dates as mm/dd/yyyy. My code is supposed to convert all of them to an specific format.
certificados$Fecha <- as.Date(certificados$Fecha,format = "%d/%m/%Y")
But im getting NAs due to date format issues.
If you cannot fix this at the source, this code finds both formats:
vec <- c("22/02/2019", "43679", "22/02/2019", "22/01/2019", "28/10/2019", "18/09/2019")
out <- as.Date(vec, format = "%d/%m/%Y")
out
# [1] "2019-02-22" NA "2019-02-22" "2019-01-22" "2019-10-28" "2019-09-18"
isna <- is.na(out)
out[isna] <- as.Date(as.integer(vec[isna]), origin = "1900-01-01")
out
# [1] "2019-02-22" "2019-08-04" "2019-02-22" "2019-01-22" "2019-10-28" "2019-09-18"

How to convert a column as date with special character?

In my dataframe I have a date column and I would like to convert it from character to date in the format d/m/y.
The head of my data:
head(df$date)
[1] [17/Jun/2019:08:33:49 [17/Jun/2019:08:38:20 [17/Jun/2019:08:38:24 [17/Jun/2019:09:52:42
[5] [17/Jun/2019:09:52:44 [17/Jun/2019:09:52:45
I used this but it converts every value into NA
df$date = as.Date(df$date, "[%d%b%y")
Try:
df$date <- strptime(df$date, format = "[%d/%b/%Y:%H:%M:%S")
df$date <- as.Date(df$date, format = "%d/%m/%y")
Using a tidyverse approach, looks like the dmy_hms() function accommodates that atypical first colon:
library(lubridate)
df <- df %>% mutate(date = dmy_hms(date), date = date(date))
Using your first value as an example:
date <- "17/Jun/2019:08:33:49"
date <- dmy_hms(date)
date
#[1] "2019-06-17 08:33:49 UTC"
date <- date(date) #or all in one line, date <- dmy_hms(date) %>% date()
date
#[1] "2019-06-17"
Assuming this is your input
x <- c("[17/Jun/2019:08:33:49", "[17/Jun/2019:08:38:20",
"17/Jun/2019:08:38:24", "[17/Jun/2019:09:52:42")
First convert it into POSIXct format and then to Date
as.Date(as.POSIXct(x, format = "[%d/%b/%Y:%T"))
#[1] "2019-06-17" "2019-06-17" "2019-06-17" "2019-06-17"
or any other format
format(as.POSIXct(x, format = "[%d/%b/%Y:%T"), "%d/%m/%Y")
#[1] "17/06/2019" "17/06/2019" "17/06/2019" "17/06/2019"
If you want to convert into Date object try this.
df$date = as.Date(df$date,format="[%d/%b/%Y:%H:%M:%S")
If you want to retain time as well, then try the following.
df$date = as.POSIXct(df$date,format="[%d/%b/%Y:%H:%M:%S")
Best wishes.

Convert dates to text in R

I have the following dataset with dates (YYYY-MM-DD):
> dates
[1] "20180412" "20180424" "20180506" "20180518" "20180530" "20180611" "20180623" "20180705" "20180717" "20180729"
I want to convert them in:
DD-MMM-YYYY but with the month being text. For example 20180412 should become 12Apr2018
Any suggestion on how to proceed?
M
You can try something like this :
# print today's date
today <- Sys.Date()
format(today, format="%B %d %Y") "June 20 2007"
where The following symbols can be used with the format( ) function to print dates 1
You need to first parse the text strings as Date objects, and then format these Date objects to your liking to have the different text output:
R> library(anytime) ## one easy way to parse dates and times
R> dates <- anydate(c("20180412", "20180424", "20180506", "20180518", "20180530",
+ "20180611", "20180623", "20180705", "20180717", "20180729"))
R> dates
[1] "2018-04-12" "2018-04-24" "2018-05-06" "2018-05-18" "2018-05-30"
[6] "2018-06-11" "2018-06-23" "2018-07-05" "2018-07-17" "2018-07-29"
R>
R> txtdates <- format(dates, "%d%b%Y")
R> txtdates
[1] "12Apr2018" "24Apr2018" "06May2018" "18May2018" "30May2018"
[6] "11Jun2018" "23Jun2018" "05Jul2018" "17Jul2018" "29Jul2018"
R>
You could use the as.Date() and format() functions:
dts <- c("20180412", "20180424", "20180506", "20180518", "20180530",
"20180611", "20180623")
format(as.Date(dts, format = "%Y%m%d"), "%d%b%Y")
More information here
Simply use as.POSIXct and as.format:
dates <- c("20180412", "20180424", "20180506")
format(as.POSIXct(dates, format="%Y%m%d"),format="%d%b%y")
Output:
[1] "12Apr18" "24Apr18" "06May18"

Add one day to every date in a days vector

I have a vector of dates called KeyDates containing two key dates. I would like to make a new vector of dates called KeyDatesPlus containing those two key dates and the two days after, in chronological order.
KeyDates <- structure(c(15159,15165), class = "Date")
#KeyDates Output:
[1] "2011-07-04" "2011-07-10"
#desired output for KeyDatesPlus:
[1] "2011-07-04" "2011-07-05" "2011-07-10" "2011-07-11"
How could I achieve that? Thank you very much.
sort(c(KeyDates, KeyDates + 1))
[1] "2011-07-04" "2011-07-05" "2011-07-10" "2011-07-11"
structure( sapply(KeyDates, "+", (0:1)), class = "Date")
[1] "2011-07-04" "2011-07-05" "2011-07-10" "2011-07-11"
Or:
as.Date( sapply(KeyDates, "+", (0:1)))
[1] "2011-07-04" "2011-07-05" "2011-07-10" "2011-07-11"
KeyDates <- structure(c(15159,15165), class = "Date")
KeyDates.plus <- as.Date(sapply(KeyDates, function(x) c(x, x+1)))
An answer using the package lubridate:
library("lubridate")
your.vector <- c("2011-07-04", "2011-07-10")
your.vector <- parse_date_time(x = your.vector, orders = "ymd")
your.vector
# [1] "2011-07-04 UTC" "2011-07-10 UTC"
one.day <- days(x = 1)
one.day
# [1] "1d 0H 0M 0S"
your.vector + one.day
# [1] "2011-07-05 UTC" "2011-07-11 UTC"
# your exact desired output (non-UTC time zone can be specified in parse_date_time):
new.vector <- sort(x = c(your.vector, your.vector + one.day))
# [1] "2011-07-04 UTC" "2011-07-05 UTC" "2011-07-10 UTC" "2011-07-11 UTC"
Lubridate distinguishes a "period" from a "duration."
A period is the time on the clock (ie if daylight savings time happens, it's what the clock reads). That's what's specified here using days().
A duration is the physical time (ie if daylight savings time happens, it's how long you've actually been sitting there.) That could be specified instead using ddays().
KeyDates <- structure(c(15159,15165), class = "Date")
KeyDatesPlus <- KeyDates+1
KeyDatesPlus <- sort(unique(c(KeyDates, KeyDatesPlus)))

Resources