Adding the ordinal indicator to a date - r

I would like to know if there is a simple way to format date with suffix on the day.
I created a function in case of:
add.suffix <- function (x) if(day(x) %in% c(1, 21, 31)) { paste0(day(x), "st", format(x, "%B %Y"))
} else if (day(x) %in% c(2, 22)){ paste0(day(x), "nd ", format(x, "%B %Y"))
} else paste0(day(x), "th ", format(x, "%B %Y"))
add.suffix(Sys.Date())
I would like as well to convert back that character in POSIXlt.

The simplest method, if you are using lubridate, is the function dmy.
dmy(add.suffix(Sys.Date()))

These do not use any packages. The examples all use this input:
x <- "9th November 2015"
1) Remove nd and th and convert the remainder to "Date" class and then "POSIXlt":
as.POSIXlt(as.Date(sub("nd|th", "", x), "%d %B %Y"))
[1] "2015-11-09 UTC"
2) This tries it with both th and nd and takes whichever works:
as.POSIXlt(na.omit(c(as.Date(x, "%dth %B %Y"), as.Date(x, "%nd %B %Y"))))
3) This is similar to the (2) but uses Filter, Negate and lapply so that we can place the alternate formats in a vector:
fmts <- c("%dth %B %Y", "%dnd %B %Y")
as.POSIXlt(Filter(Negate(is.na), lapply(fmts, as.Date, x = x))[[1]])
You might prefer to just convert this to "Date" class rather than "POSIXlt" since it is only a date. Just omit the as.POSIXlt part in any of the above approaches.

Related

Convert to date from an unusual format [duplicate]

folk!
R can not recognize the next type of date: "Jun 3, 1986"
I tried it several times converting that column to factor or character.
The next examples from stackoverflow do not work and return "NA":
sdate2 <- "jan151999"
ndate2 <- as.Date(sdate2, "%B%d%Y"); ndate2
dates <- c("May 27 1984", "July 7 2005")
betterDates <- as.Date(dates,
format = "%B %d %Y")
I used small letter %b and it did not work also.
My version does not work as well
as.Date("Jun 3, 1986", format = "%b %d, %Y")
Could you help me?
That is probably because your locale language is different. Change it to English and it should work.
Sys.setlocale("LC_ALL","English")
as.Date("Jun 3, 1986", format = "%b %d, %Y")
#[1] "1986-06-03"
And also this way works well. SOme oone suggested it but deleted afterward.
lubridate::mdy("Jun 3, 1986")

R can not recognize date in format

folk!
R can not recognize the next type of date: "Jun 3, 1986"
I tried it several times converting that column to factor or character.
The next examples from stackoverflow do not work and return "NA":
sdate2 <- "jan151999"
ndate2 <- as.Date(sdate2, "%B%d%Y"); ndate2
dates <- c("May 27 1984", "July 7 2005")
betterDates <- as.Date(dates,
format = "%B %d %Y")
I used small letter %b and it did not work also.
My version does not work as well
as.Date("Jun 3, 1986", format = "%b %d, %Y")
Could you help me?
That is probably because your locale language is different. Change it to English and it should work.
Sys.setlocale("LC_ALL","English")
as.Date("Jun 3, 1986", format = "%b %d, %Y")
#[1] "1986-06-03"
And also this way works well. SOme oone suggested it but deleted afterward.
lubridate::mdy("Jun 3, 1986")

Getting a handle on date conversion in R

I created a column of dates by hand.
Need to convert entire column Date to xx/xx/xxxx (01/dd/2012)
I know how to use substrings to extract month day year from a standard data class like 01JAN12
DateM=substr(Date,START,FINISH)
DateM
Now put it together as xx/xx/xx
Here is my code:
Date=c("January 23,2012","January 24,2012","January
25,2012","January 26,2012","January 27,2012")
# WANT: 23JAN12 24JAN12 25JAN12 26JAN12 27JAN12
Date3=format(Date,"%B %d %Y")
# Error in format.default(Date, "%B %d %Y") : invalid 'trim'
argument but I still get the dates ok when I print out
#WANT: 01/23/2012 01/24/2012 01/25/2012 01/26/2012 01/27/2012 from
Date3
Date4=format(Date3,"%B %d %Y")
invalid 'trim' argument
# WANT: 2012-01-23 2012-01-24 2012-01-25 2012-01-26 2012-01-27
Date5=format(Date3,"%Y %B %d")
invalid 'trim' argument
I want to use the basic functions of R as opposed to lubridate.
Can someone direct me how to finish this, please?
You should use as.Date and not format
as.Date(Date, "%B %d, %Y")
#[1] "2012-01-23" "2012-01-24" "2012-01-25" "2012-01-26" "2012-01-27"
as.Date is used to convert dates from different character representation to "Date" class.
Once you have data in the "Date" class, we can then use format to represent them in the way we want.
format(as.Date(Date, "%B %d, %Y"), "%m/%d/%Y")
#[1] "01/23/2012" "01/24/2012" "01/25/2012" "01/26/2012" "01/27/2012"

Converting string dates to numeric dates

I want to convert this kind of dates :Apr 09, 2019 to this kind of dates: Apr 09, 2019-04-09
I wrote
as.Date(Data$date, format = "%B %d, %Y")
format(as.Date(Data$date, format = "%B %d, %Y"), "%d-%m-%Y")
That code worked, however when I View(Data) I see that it had not converted.
Why? Any idea?
The reason is that the column is not updated. We need to assign (<-) the results back to the original column or a new column
Data$date <- format(as.Date(Data$date, format = "%B %d, %Y"), "%d-%m-%Y")

Converting written to numeric dates in R

What's the most elegant way to convert these example dates to numeric dates:
dates <- c("April 1, 2017", "June 27, 2017", "September 24, 2017")
I would like this as a result:
"01-04-2017", "27-06-2017", "24-09-2017"
Using base
as.Date(dates, format = "%B %d, %Y")
[1] "2017-04-01" "2017-06-27" "2017-09-24"
and then formatted
format(as.Date(dates, format = "%B %d, %Y"), "%d-%m-%Y")
[1] "01-04-2017" "27-06-2017" "24-09-2017"
You could use mdy function of lubridate package to parse dates. Then use format to convert it to desired form.
library(lubridate)
format(mdy(dates), "%d-%m-%Y")
#[1] "01-04-2017" "27-06-2017" "24-09-2017"

Resources