Converting a date in R returns NA - r

date
05-06-2016
05-07-2016
4/13/2016
4/14/2016
I want to format the column to date format using below code
td3 <- read.csv("Book2.csv")
td3$date <- as.Date(td3$date, "%m-%d-%y")
when i run the code the last 2 rows return NA

as.Date.character(gsub("/", "-",td3$date), '%m-%d-%Y')
[1] "2016-05-06" "2016-05-07" "2016-04-13" "2016-04-14"

Here is a solution with parse_date_time from lubridate package:
library(lubridate)
as.Date(parse_date_time(df$date, orders = c('mdy', 'dmy')))
[1] "2016-05-06" "2016-05-07" "2016-04-13" "2016-04-14"

Related

Can a datepart be parsed as date object, directly without concatenating the missing datepart

Suppose I have a character vector dt with some months-years stored as character format-
dt <- c("Mar-19", "Apr-19", "May-19")
when I try to convert it into date object, it returns NAs only
as.Date(dt, format = "%b-%y")
[1] NA NA NA
So I have to first concatenate a dummy date say 01 to each object and then parse it as date and thereafter format the vector to show it in original format
format.Date(as.Date(paste0("01-", dt), format = "%d-%b-%y"), "%b-%y")
[1] "Mar-19" "Apr-19" "May-19"
Is there any direct method to parse a truncated date/datepart directly without concatenating and thus avoiding the long route?
zoo has as.yearmon class and function which can convert data with year and month without the date.
dt <- c("Mar-19", "Apr-19", "May-19")
zoo::as.yearmon(dt, '%b-%y')
To get it in character class you can use format with format as required.
format(zoo::as.yearmon(dt, '%b-%y'), '%b-%y')
#[1] "Mar-19" "Apr-19" "May-19"
You can use readr::parse_date for this, but it is still not elegant
library(tidyverse)
dt <- c("Mar-19", "Apr-19", "May-19")
dt %>% parse_date(format = "%b-%y") %>% format("%b-%y")
#> [1] "Mar-19" "Apr-19" "May-19"
Created on 2021-02-01 by the reprex package (v0.3.0)
dt <- c("Mar-19", "Apr-19", "May-19")
lubridate::myd(dt, truncated = 1)
#> [1] "2019-03-01" "2019-04-01" "2019-05-01"
# Created on 2021-02-01 by the reprex package (v0.3.0.9001)
(Related to Conversion of date format %B %Y)
Regards,

Convert dates of varying formats at once using dplyr

Let's say there is a list like this:
date
17-Dec-19
7/26/2018
02/01/2019
02-Mar-18
I suppose I could do several ifelse statements but is there a way I could just get them all formatted at once to look like:
date
2020-12-19
2018-07-26
2019-02-01
2018-03-02
It may be easier to convert to date with anydate from anytime
library(anytime)
df1$date <- anydate(df1$date)
df1$date
#[1] "2019-12-17" "2018-07-26" "2019-02-01" "2018-03-02"
Or with parse_date_time
library(lubridate)
as_date(parse_date_time(df1$date, c("dmy", "mdy")))

how to convert factor column into date in data frame in r

I have a data frame in r with the following structure:
TERMINAL_ID ACTION_DATE ACC_AMOUNT
1009162 02-JAN-18 14.30
1009162 02-JAN-18 21.45
and the class for 'action_date' is factor and I would to convert it to date.
I tried this code but no success
dataf <- as.Date(as.character(data$ACTION_DATE),
format = "%d-%y-%Y")
fdate=as.POSIXlt(data$ACTION_DATE, "%d-%b-%y")
or by using lubridate
library(lubridate)
dmy("02-Jan-17")
this turns your character which holds the date in d(ays)m(onth)y(ear) into a Date format.
as.Date('02-JAN-18', format = '%d-%B-%y')
[1] "2018-01-02"
I have created vector x with date format you mentioned. Using the lubridate package, the solution was pretty straight forward.
x<- as.factor(c("02-JAN-18", "03-JAN-19", NA, "abc"))
lubridate::dmy(x)
#> Warning: 1 failed to parse.
#> [1] "2018-01-02" "2019-01-03" NA NA
Created on 2018-10-01 by the reprex package (v0.2.1)

Fixing date format in R

I have three data tables in R. Each one has a date column. The tables are vix_data,gold_ohlc_data,btc_ohlc_data. They are formatted as follows:
head(vix_data$Date)
[1] 1/2/04 1/5/04 1/6/04 1/7/04 1/8/04 1/9/04
3435 Levels: 1/10/05 1/10/06 1/10/07 1/10/08 1/10/11 ... 9/9/16
head(gold_ohlc_data$date)
[1] 8/23/17 8/22/17 8/21/17 8/18/17 8/17/17 8/16/17
2519 Levels: 1/10/08 1/10/11 1/10/12 1/10/13 1/10/14 ... 9/9/16
head(btc_ohlc_data$Date)
[1] "2017-08-23" "2017-08-22" "2017-08-21" "2017-08-20" "2017-08-19"
[6] "2017-08-18"
How can I change the date column in the vix_data and gold_ohlc_data tables to match the btc_ohlc_data format? I have tried several methods, for example using as.Date to transform each column- but this usually messes up the values and inserts a lot of N/A's
An option is to use functions from the package lubridate. The users need to know which one is day and which one is month to select the right function to use, such as dmy or mdy
# Load package
library(lubridate)
# Create example string
date1 <- c("1/2/04", "1/5/04", "1/6/04", "1/7/04", "1/8/04", "1/9/04")
date2 <- c("8/23/17", "8/22/17", "8/21/17", "8/18/17", "8/17/17", "8/16/17")
# Convert to date class
dmy(date1)
# [1] "2004-02-01" "2004-05-01" "2004-06-01" "2004-07-01" "2004-08-01" "2004-09-01"
mdy(date1)
# [1] "2004-01-02" "2004-01-05" "2004-01-06" "2004-01-07" "2004-01-08" "2004-01-09"
mdy(date2)
# [1] "2017-08-23" "2017-08-22" "2017-08-21" "2017-08-18" "2017-08-17" "2017-08-16"
Look into the package lubridate. lubridate::dmy() and ymd() should handle this just fine.
It looks like your data are read in as factors, so first you'll have to change them to characters. Then after that you can convert it to a date and specify the input format where %m represents the numerical month, %d represents the day, and %y represents the 2-digit year.
x <- c('1/2/04', '1/5/04', '1/6/04', '1/7/04', '1/8/04', '1/9/04')
y <- as.Date(x, format = "%m/%d/%y")
y
[1] "2004-01-02" "2004-01-05" "2004-01-06" "2004-01-07" "2004-01-08"
[6] "2004-01-09"
Are you sure you're specifying as.Date correctly? For example, do you have %y, instead of %Y?
I did the following and it worked:
> vix <- c("1/2/04", "1/5/04", "1/6/04", "1/7/04", "1/8/04", "1/9/04")
> vix<- as.factor(vix)
> vix
[1] 1/2/04 1/5/04 1/6/04 1/7/04 1/8/04 1/9/04
Levels: 1/2/04 1/5/04 1/6/04 1/7/04 1/8/04 1/9/04
> as.Date(vix, "%m/%d/%y")
[1] "2004-01-02" "2004-01-05" "2004-01-06" "2004-01-07" "2004-01-08" "2004-01-09"

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.

Resources