Change column with different formats into dates - r

Would like to change a column of my data.frame into the date format in R.
The problem is that the format of the column is not consistent.
Most rows are in the format "%Y-%m-%d" and I can change them easily with the as.Date() function.
Few rows are in the format "%Y/%d/%m" and can't change them with the as.Date() function but instead I get NA's.
input <- c("2019-01-22", "2019-04-17", "2019/27/05", "2019/13/05", "2019/15/06", "2019-07-30")
Input: Output:
Dates Dates
2019-01-22 2019-01-22
2019-04-17 2019-04-17
2019/27/05 2019-27-05
2019/13/05 2019-13-05
2019/15/06 2019-15-06
2019-07-30 2019-07-30

In your case, in which you have "%Y-%m-%d" and "%Y/%d/%m", you might to use as.Date including the format it has. So, for example:
input <- c("2019-10-11", "2019/27/10", "2014-12-10")
If you use:
input2 <- ifelse(grepl("/",input), format(as.Date(input,"%Y/%d/%m"),"%Y-%m-%d"), input)
then:
> input2
[1] "2019-10-11" "2019-10-27" "2014-12-10"

If you have only these two formats you can substitute all the / for -:
Example:
input <- c("2019-10-11", "2019/10/12", "2014-10-13")
as.Date(gsub("/", "-", input), format = "%Y-%m-%d")
# [1] "2019-10-11" "2019-10-12" "2014-10-13"

If you just want to replace / by - for just a few rows, I think the following code might be an efficiency way to do the replacement
output <- as.Date(input)
output[is.na(output)]<-as.Date(input[is.na(output)],format = "%Y/%d/%m")
such that
> output
[1] "2019-01-22" "2019-04-17" "2019-05-27" "2019-05-13" "2019-06-15" "2019-07-30"

We can use anydate from anytime
library(anytime)
anydate(input)
#[1] "2019-10-11" "2019-10-12" "2014-10-13"
Or using lubridate
library(lubridate)
ymd(input)
data
input <- c("2019-10-11", "2019/10/12", "2014-10-13")

Related

How to parse both of these date formats?

Dates formatted as 4/29/2016 are parsed correctly, but dates formatted as 6242016 and 2042016 are not parsed.
Does R think that some of the dates without the slash have day first instead of month?
I've tried including dmy in lubrdiate and it still doesn't work.
I've tried looking at Sys.getlocale("LC_TIME") and it gives me "English_United States.1252".
demo$date <- as.character(demo$date)
demo <- demo %>%
mutate(date = parse_date_time(date, "mdy"))
You can wrangle your dates all into the same format using stringr. Then convert to numeric and use lubridate to parse.
library(stringr)
library(lubridate)
dates <- c("6242016", "2042016", "4/29/2016")
dates <- str_remove_all(dates, "/")
dates <- as.numeric(dates)
lubridate::mdy(dates)
# [1] "2016-06-24" "2016-02-04" "2016-04-29"
as.Date(sprintf("%08d",
as.numeric(gsub("/", "", c("6242016", "2042016", "4/29/2016")))),
format = "%m%d%Y")
# [1] "2016-06-24" "2016-02-04" "2016-04-29"
This
as.Date("2042016", "%m%d%Y")
returns NA as opposed to
as.Date("02042016", "%m%d%Y")
This is because the month has to be represented by two digits (00-12)
Try adding a leading zero for months in the range [1,9].

how to split numbers

I have a date format like this
5170301, where it means 1st March 2017.And I have 5 attached to it
I want the format of the date to be changed.
So can anyone help me in splitting that 5 from the date?
We can use substring to read from the 2nd character onwards
v1 <- substring(df1$date, 2)
NOTE: It should work for numeric/character/factor class
Then we change it to Date class
v2 <- as.Date(v1, "%y%m%d")
and if needed change the format
format(v2, "%d %b %Y")
Or as #thelatemail mentioned, it can be mentioned in the format
as.Date(df1$date, "5%y%m%d")
You can split it quite nicely with the stringr package
Split <- stringr::str_split_fixed(string=Column_Name, pattern="5", n=2)
This will give two variables: one blank and one of your value after the "5" (170301)
Then can change it to the date as so:
Date1 <- as.Date(format="%d%m%y", x = Split)

How to convert a date to YYYYDDD?

I can't figure out how to turn Sys.Date() into a number in the format YYYYDDD. Where DDD is the day of the year, i.e. Jan 1 would be 2016001 Dec 31 would be 2016365
Date <- Sys.Date() ## The Variable Date is created as 2016-01-01
SomeFunction(Date) ## Returns 2016001
You can just use the format function as follows:
format(Date, '%Y%j')
which gives:
[1] "2016161" "2016162" "2016163"
If you want to format it in other ways, see ?strptime for all the possible options.
Alternatively, you could use the year and yday functions from the data.table or lubridate packages and paste them together with paste0:
library(data.table) # or: library(lubridate)
paste0(year(Date), yday(Date))
which will give you the same result.
The values that are returned by both options are of class character. Wrap the above solutions in as.numeric() to get real numbers.
Used data:
> Date <- Sys.Date() + 1:3
> Date
[1] "2016-06-09" "2016-06-10" "2016-06-11"
> class(Date)
[1] "Date"
Here's one option with lubridate:
library(lubridate)
x <- Sys.Date()
#[1] "2016-06-08"
paste0(year(x),yday(x))
#[1] "2016160"
This should work for creating a new column with the specified date format:
Date <- Sys.Date
df$Month_Yr <- format(as.Date(df$Date), "%Y%d")
But, especially when working with larger data sets, it is easier to do the following:
library(data.table)
setDT(df)[,NewDate := format(as.Date(Date), "%Y%d"
Hope this helps. May have to tinker if you only want one value and are not working with a data set.

How to separate date and time

I`m giving an input as "a <- [12/Dec/2014:05:45:10]"
a is not a time-stamp so cannot use any time and date functions
Now I want the above variable to be split down into 2 parts as:-
date --> 12/Dec/2014
time --> 05:45:10
Any help will be appreciated.
You can use gsub to create a space between the Date and Time and use that to create two columns with read.table
read.table(text=gsub('^\\[([^:]+):(.*)+\\]', '\\1 \\2', a),
sep="", col.names=c('Date', 'Time'))
# Date Time
# 1 12/Dec/2014 05:45:10
Or you can use lubridate to convert it to a 'POSIXct' class
library(lubridate)
a1 <- dmy_hms(a)
a1
#[1] "2014-12-12 05:45:10 UTC"
If we need two columns with the specified format
d1 <- data.frame(Date= format(a1, '%d/%m/%Y'), Time=format(a1, '%H:%M:%S'))
data
a <- "[12/Dec/2014:05:45:10]"
Code
a <- "[12/Dec/2014:05:45:10]"
Sys.setlocale("LC_TIME", "C") # depends on your local setting
as.POSIXlt(a, format = "[%d/%b/%Y:%H:%M:%S]")
Explanation
Depending on your local setting you need to change it such that the abbreviated month names can be read. Then you can use as.POSIXlt together with the format string to convert your string in a date.

Format two kinds of factor dates in R

I have two sets of date looking strings; either 31.3.14 or 31/3/14
I would like to format them to 31-3-2014
Now I know how to format each of them to desired format, but I don't know how to distinguish them and apply the approach bellow.
For this format 31.3.14 :
format(as.Date(as.character("31.3.14"), "%d.%m.%y"), "%d-%m-%Y")
For this format 31/3/14 :
format(as.Date(as.character("31/3/14"), "%d/%m/%Y"), "%d-%m-%Y"))
I have this sorts of dates in a dataframe column randomly so I would need to apply given method for the right set of format.
EDIT: sorry I have also different kinds of dates, also: "2013-04-01" here the solution provided with dmy function fails.
Could also do it with base R by removing punctuations first
Dates <- c("31.3.14", "31/3/14")
format(as.Date(gsub("[[:punct:]]", "-", Dates), format = "%d-%m-%y"), "%d-%m-%Y")
## [1] "31-03-2014" "31-03-2014"
Hadley Wickham's Lubridate package makes this easy.
> require(lubridate)
> test <- data.frame(raw = c("31.3.14", "31/3/14"))
> test$formatted <- dmy(test$raw)
> test
raw formatted
1 31.3.14 2014-03-31
2 31/3/14 2014-03-31
EDIT:
Based on the edit to the question, one can use ifelse() within a function to detect a four-digit sequence at the start of the date string.
require(stringr)
myDateFun <- function(x){
z <- ifelse(str_detect(x, "^\\d{4}") == TRUE,
ymd(x), dmy(x) )
z <- as.POSIXlt(z, origin = "1970-01-01")
z <- format(z, "%Y-%m-%d")
return(z)
}
test <- data.frame(raw = c("31.3.14", "31/3/14", "2014-3-31"))
test$formatted.2 <- myDateFun(test$raw)
test
raw formatted formatted.2
1 31.3.14 2014-03-31 2014-03-31
2 31/3/14 2014-03-31 2014-03-31
3 2014-3-31 <NA> 2014-03-31

Resources