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")
Related
I need help in R program to change date format from "May 29, 2015" to "05,29,2015".I tried this "housing$SaleDate<- as.Date(housing$SaleDate,"%Y%M%D")" but did not work. Thanks
After converting to Date class, we may need format i.e.
library(lubridate)
str1 <- "May 29, 2015"
format(mdy(str1), '%m,%d,%Y')
[1] "05,29,2015"
For the OP's as.Date, it would be
housing$SaleDate <- format(as.Date(housing$SaleDate, "%b %d, %Y"), "%m,%d,%Y")
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")
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")
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"
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.