Convert numeric to specific date format in R - r

I am trying to convert a numeric column "DATE" to date column "NEW_DATE" in format YYYYMM:
DATE NEW_DATE
1.2020 202001
I have tried as.Date but it returns the following error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
The structure function says that the column is num so not sure how to proceed.

We can use zoo's as.yearmon and then convert it with format.
format(zoo::as.yearmon('1.2020', "%m.%Y"), "%Y%m")
#[1] "202001"
So for the dataframe, we can do :
df$NEW_DATE <- format(zoo::as.yearmon(df$DATE, "%m.%Y"), "%Y%m")
Or in base R we can paste an arbitrary date and convert to date.
df$NEW_DATE <- format(as.Date(paste0(df$DATE, ".1") "%m.%Y.%d"), "%Y%m")

Base R solution:
# Isolate the month:
date_month <- gsub("\\..*", "", df$DATE)
# Store the NEW_DATE vector in desired format:
df$NEW_DATE <- paste0(gsub(".*\\.", "", df$DATE),
ifelse(nchar(date_month) == 1, paste0("0", date_month), date_month))
# Data:
df <- data.frame(DATE = "1.2020", stringsAsFactors = FALSE)

Related

Custom Date Transformation in R

Using R
How do we convert "yyyymm" to "yyyy-mm-01" across all the rows?
Eg: "201603" to "2016-03-01" (ie "yyyy-mm-dd" format)
PS: Here, (dd = 01) is the default date for all 12 months. ie("2016-01-01" , "2016-02-01" , etc...)
A simple paste solution:
x <- "201603"
paste0(substr(x, 1,4), "-", substr(x, 5,6), "-01")
[1] "2016-03-01"
If you want to transform as date:
as.Date(paste0(201603, 01), format = "%Y%m%d")
This will create the 2016-03-01 format as the date and not as a character.
If you want to use in all rows on column Date
data <- data %>%
mutate(Date = as.Date(paste(Date, 01) format = "%Y%m%d")
additional solution
library(lubridate)
library(stringr)
x <- c("201603")
ymd(str_c(x,"01"))
[1] "2016-03-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.

How to strip time from date time into DDMMYYYY from DDMMYYY:HH:MM:SS

I have a data frame with a column that is filled with dates time.
str(df$date)
char [1:2000] "01JAN2015:12:20:00" "19JUN2015:15:30:00" "19OCT2013:05:26:00"
I want to strip time and just have the column being left with DDMMYYY such as
01JAN2015 19JUN2015 19OCT2013
I have tried df$date <- as.POSIXct(df$date) and the output was
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
or the df$date <- as.Date(df$date) and the output was
Error in charToDate(x) :
character string is not in a standard unambiguous format
I don't know what I am doing wrong. I would greatly appreciate help.
Thanks
Using lubridate:
df$date <- as.Date(df$date, "%d%b%Y")
will give you:
[1] "2015-01-01" "2015-06-19" "2013-10-19"
Hope it helps you
This will do it
library(lubridate)
dates <- c("01JAN2015:12:20:00", "19JUN2015:15:30:00", "19OCT2013:05:26:00")
dates <- sub(":.*$", "", dates) %>% dmy(.) %>% gsub("-", "", .)
paste0(substr(dates, 7, 8), substr(dates, 5, 6), substr(dates, 1, 4))
# [1] "01012015" "19062015" "19102013"

Convert yyyymmdd string to Date class in R

I would like to convert these dates with format YYYYMMDD to a Date class.
dates <- data.frame(Date = c("20130707", "20130706", "20130705", "20130704"))
I tried:
dates <- as.Date(dates, "%Y%m%d")
And I get the following error:
Error in as.Date.default(dates, "%Y%m%d") :
do not know how to convert 'dates' to class "Date"
What would be the correct way to set this format?
You need to provide the Date column, not the entire data.frame.
R> as.Date(dates[["Date"]], "%Y%m%d")
[1] "2013-07-07" "2013-07-06" "2013-07-05" "2013-07-04"
An extra conversion to characters works for me:
dates<-as.Date(as.character(dates),format="%Y%m%d")
Without the conversion the following error occurs:
dates<-as.Date(dates,format="%Y%m%d")
Error in as.Date.numeric(dates, format = "%Y%m%d") :
'origin' must be supplied
Different error but this might help, works for POSIXct too, paste date and hours, format %Y%m%d%H
Classic R:
> start_numeric <- as.Date('20170215', format = '%Y%m%d');
> start_numeric
[1] "2017-02-15"
> format(start_numeric, "%Y%m%d")
[1] "20170215"
Use the lubridate package for an easy conversion:
date_test <- data.frame(Date = c("20130707", "20130706", "20130705", "20130704"))
date_test$Date <- ymd(date_test$Date)
date_test
Date
1 2013-07-07
2 2013-07-06
3 2013-07-05
4 2013-07-04
Instead of using brackets, you can use variable name:
dates <- data.frame(Date = c("20130707", "20130706", "20130705", "20130704"))
as.Date(dates$Date, "%Y%m%d")
[1] "2013-07-07" "2013-07-06" "2013-07-05" "2013-07-04"

Resources