How to reformat dates without NA's - r

I have a column of dates I need to reformat, they are in the format m/d/yyyy, but as the dates grow the format becomes mm/dd/yyyy. When I try to run
as.Date(x, format = '%m/%d/%y')
I get NA's
How can I reformat the dates to mm/dd/yyyy?

Here is an example:
# convert date info in format 'mm/dd/yyyy'
strDates <- c("01/05/1965", "08/16/1975")
strDates
Output:
[1] "01/05/1965" "08/16/1975"
Convert the dates using this code:
dates <- as.Date(strDates, "%m/%d/%Y")
Output:
[1] "1965-01-05" "1975-08-16"

Related

Getting Wrong date format as result in R

I am trying to separate a date from a column in a database, but the result date format is not proper.
column data = "24-01-2021 19:15"
Code used:
database_1$date <- format(as.Date(database_1$start_time), "%d-%m-%Y")
Result: 20-01-0024
Expected result: 24-01-2021
Update:
I just realized the expected output:
just add format("%d.%m.%Y") to the code:
as.Date(dmy_hm(column_date)) %>%
format("%d.%m.%Y")
[1] "24.01.2021"
With lubridate package you can:
With dmy_hm you read in the character column_date to date format
Then you can add as.Date
library(lubridate)
column_date <- "24-01-2021 19:15"
as.Date(dmy_hm(column_date))
[1] "2021-01-24"

Change format of column in R

In my table I have a timestamp column in which i want to change the format. I already tried multiple things but it doesnt work. I also can not find examples to change the format in the whole column. First i converted the column to POSIXct then Ive tried to adjsut the format:
#timestamp as POSIXct and lane as numeric
Flows_ALM_2019 %>%
mutate(timestamp = as.POSIXct(timestamp),lane = as.numeric(lane)) -> Flows_ALM_2019
#remove seconds from time in timestamp column
df <- strftime(timestamp, format= "%Y-%m-%d %H:%m")
after trying:
df_2019_tsb$timestamp <- format(df_2019_tsb$timestamp,format= "%Y-%m-%d %H:%m")
This is what I had:
and now it changed to:
What am i doing wrong?
You can use format :
Flows_ALM_2019$timestamp <- format(Flows_ALM_2019$timestamp, "%Y-%m-%d %H:%m")
Or with strftime.
Flows_ALM_2019$timestamp <- strftime(Flows_ALM_2019$timestamp, "%Y-%m-%d %H:%m")
This is assuming that timestamp column is of class POSIXct in your data.

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.

How to change date format from YYYY/MM/DD to DD/MM/YYYY

I have a column of dates which were read in as character values (yes, they are supposed to be the same):
str(df$date)
$ date : chr "30/08/2017" "30/08/2017" "30/08/2017" "30/08/2017"
I then convert the values to Date format:
str(df$date)
$ date : Date, format: "2017-08-30" "2017-08-30" "2017-08-30"
The problem is that no matter which method I choose to use, the resulting dates are always in YYYY/MM/DD format, which is not what I want; they should be in DD/MM/YYYY format.
I try:
df$date <- as.Date(df$date, format = "%d/%m/%Y")
df$date <- strptime(df$date, format = "%d/%m/%Y")
df$date <- as.POSIXct(df$date, format = "%d/%m/%Y")
and they all produce the same format.
I have read numerous similar Stack Overflow posts as well as some guides and have tried things like getting and setting my system locale (United Kingdom) and all is correct in that respect.
Where am I going wrong?
I try:
df$date <- as.Date(df$date, format = "%d/%m/%Y")
df$date <- strptime(df$date, format = "%d/%m/%Y")
df$date <- as.POSIXct(df$date, format = "%d/%m/%Y")
and they all produce the same format.
R has two very similarly named functions: strptime, which converts from character strings to Date data, and strftime, which converts Dates to formatted strings. To make matters worse, the documentation for these two functions is combined, so it can be very hard to keep their uses straight. You want strftime, in this case.
You can also use format:
date = c("30/08/2017", "30/08/2017", "30/08/2017", "30/08/2017")
date <- as.Date(date, format = "%d/%m/%Y")
# > date
# [1] "2017-08-30" "2017-08-30" "2017-08-30" "2017-08-30"
date = format(date, "%m/%d/%Y")
# > date
# [1] "08/30/2017" "08/30/2017" "08/30/2017" "08/30/2017"
Turns into character class:
# > class(date)
# [1] "character"
This will help you:
$variable = '2018/09/18';
$date = str_replace('/', '/', $variable);
echo date('d/m/Y', strtotime($date));
Please check and let me know if you need any more help.

Resources