I have the following column in my dataframe
> df$dates
[1] "01APR2020" "01JUN2020" "01MAR2020" "01MAY2020" "02APR2020" "02JUN2020"
[7] "02MAR2020"
I would like to format this to an object of Date class, so I want my output to look like this
> df$dates
[1] "01-04" "01-06" "01-03" "01-05" "02-04" "02-06"
[7] "02-03"
And I would like to order them from the oldest to the newest.
Edit:
For example I tried this but it doesn't work:
> format(as.Date("01APR2020", "%d%b%Y"), "%d-%m")
[1] NA
Thanks!
Just use the anydate() function from the anytime package
R> anydate(c("01APR2020", "01JUN2020", "01MAR2020"))
[1] "2020-04-01" "2020-06-01" "2020-03-01"
R>
It's idea is to not require a format for a variety of common and sensible date (or datetime) inputs. Once they are parsed, putting out day and months is easy too:
R> format(anydate(c("01APR2020", "01JUN2020", "01MAR2020")), "%d-%m")
[1] "01-04" "01-06" "01-03"
R>
We can use as.Date with format
df$dates <- format(as.Date(df$dates, "%d%b%Y"), "%d-%m")
df$dates
#[1] "01-04" "01-06" "01-03" "01-05" "02-04" "02-06" "02-03"
Or using lubridate
library(lubridate)
df$dates <- format(dmy(df$dates), "%d-%m")
NOTE: Both the solutions work on R 4.0
data
df <- data.frame(dates = c("01APR2020" ,"01JUN2020", "01MAR2020",
"01MAY2020", "02APR2020" ,"02JUN2020" , "02MAR2020"))
Related
I am trying to convert the following numbers to dates in R:
Use as.Date with the correct format mask:
dates <- c(19801231, 19810130, 19810227)
as.Date(as.character(dates), format="%Y%m%d")
[1] "1980-12-31" "1981-01-30" "1981-02-27"
Note that if your Names.Date column already be text, then you can skip the extra conversion and just use:
as.Date(dates, format="%Y%m%d")
You can use lubridate package.
> library(lubridate)
> Names.Date <- c(19810130, 19810227, 19810331)
> Names.Date
[1] 19810130 19810227 19810331
> ymd(Names.Date)
[1] "1981-01-30" "1981-02-27" "1981-03-31"
>
An option with anydate
library(anytime)
anydate(dates)
#[1] "1980-12-31" "1981-01-30" "1981-02-27"
data
dates <- c(19801231, 19810130, 19810227)
I imported a CSV file with dates from a SQL query, but the dates are really date-time values and R doesn't seem to recognize them as dates:
> mydate
[1] 1/15/2006 0:00:00
2373 Levels: 1/1/2006 0:00:00 1/1/2007 0:00:00 1/1/2008 0:00:00 ... 9/9/2012 0:00:00
> class(mydate)
[1] "factor"
> as.Date(mydate)
Error in charToDate(x) :
character string is not in a standard unambiguous format
How do I convert mydate to date format? (I don't need to include the time portion.)
You were close. format= needs to be added to the as.Date call:
mydate <- factor("1/15/2006 0:00:00")
as.Date(mydate, format = "%m/%d/%Y")
## [1] "2006-01-15"
You can try lubridate package which makes life much easier
library(lubridate)
mdy_hms(mydate)
The above will change the date format to POSIXct
A sample working example:
> data <- "1/15/2006 01:15:00"
> library(lubridate)
> mydate <- mdy_hms(data)
> mydate
[1] "2006-01-15 01:15:00 UTC"
> class(mydate)
[1] "POSIXct" "POSIXt"
For case with factor use as.character
data <- factor("1/15/2006 01:15:00")
library(lubridate)
mydate <- mdy_hms(as.character(data))
Take a look at the formats in ?strptime
R> foo <- factor("1/15/2006 0:00:00")
R> foo <- as.Date(foo, format = "%m/%d/%Y %H:%M:%S")
R> foo
[1] "2006-01-15"
R> class(foo)
[1] "Date"
Note that this will work even if foo starts out as a character. It will also work if using other date formats (as.POSIXlt, as.POSIXct).
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"
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"
I imported a CSV file with dates from a SQL query, but the dates are really date-time values and R doesn't seem to recognize them as dates:
> mydate
[1] 1/15/2006 0:00:00
2373 Levels: 1/1/2006 0:00:00 1/1/2007 0:00:00 1/1/2008 0:00:00 ... 9/9/2012 0:00:00
> class(mydate)
[1] "factor"
> as.Date(mydate)
Error in charToDate(x) :
character string is not in a standard unambiguous format
How do I convert mydate to date format? (I don't need to include the time portion.)
You were close. format= needs to be added to the as.Date call:
mydate <- factor("1/15/2006 0:00:00")
as.Date(mydate, format = "%m/%d/%Y")
## [1] "2006-01-15"
You can try lubridate package which makes life much easier
library(lubridate)
mdy_hms(mydate)
The above will change the date format to POSIXct
A sample working example:
> data <- "1/15/2006 01:15:00"
> library(lubridate)
> mydate <- mdy_hms(data)
> mydate
[1] "2006-01-15 01:15:00 UTC"
> class(mydate)
[1] "POSIXct" "POSIXt"
For case with factor use as.character
data <- factor("1/15/2006 01:15:00")
library(lubridate)
mydate <- mdy_hms(as.character(data))
Take a look at the formats in ?strptime
R> foo <- factor("1/15/2006 0:00:00")
R> foo <- as.Date(foo, format = "%m/%d/%Y %H:%M:%S")
R> foo
[1] "2006-01-15"
R> class(foo)
[1] "Date"
Note that this will work even if foo starts out as a character. It will also work if using other date formats (as.POSIXlt, as.POSIXct).