How to convert string "MMM DD YYYY" to date YYYY-MM-DD - r

I received Excel spreadsheet with text "MMM DD YYYY" for a date column.
Unfortunately, this needs to be dumped into R. Anyone can help convert this to R date?
excel string Jan 05 2004 to r date 2004-01-05
Thanks

We can use as.Date with the format argument
df1$Date <- as.Date(df1$Date, "%b %d %Y")
df1$Date
#[1] "2004-01-05" "2004-01-06"
Or with lubridate
library(lubridate)
mdy(df1$Date)
Or automaticaly pick te format with anydate
library(anytime)
anydate(df1$Date)
data
df1 <- data.frame(Date = c("Jan 05 2004", "Jan 06 2004"), stringsAsFactors = FALSE)

Related

in R, modify date from aug 07, 2020 to 08,07,2020, also how to remove time zone

[Fri Aug 07, 2020 05:12 UTC]
I have this date format in a column, how to modify it to be 08, 07,2020 05:12
also, how to remove UTC from all columns
Check ?strptime for various format options. First convert the data to POSIXct, you can then use format to get it any format that you want.
x <- 'Fri Aug 07, 2020 05:12 UTC'
x1 <- as.POSIXct(x, format = '%a %b %d, %Y %H:%M UTC', tz = 'UTC')
x1
#[1] "2020-08-07 05:12:00 UTC"
format(x1, '%m,%d,%Y %H:%M')
#[1] "08,07,2020 05:12"
If we want to apply this for multiple columns we can use lapply. For example for first 4000 columns where your dataframe is called df we can do :
cols <- 1:4000
df[cols] <- lapply(df[cols], function(x) format(as.POSIXct(x,
format = '%a %b %d, %Y %H:%M UTC', tz = 'UTC'), '%m,%d,%Y %H:%M'))

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")

as.Date returning NA while converting it from character

I am converting following format to date from character
January 2016
I want to convert it to following format
201601
I am using following code
df$date <- as.Date(df$date,"%B %Y")
But it returns me NA values. I have even set the locale as follows
lct<- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME",lct)
But it gives me NA values. How to fix it
We can do this easily with as.yearmon and format
library(zoo)
format(as.yearmon(str1), "%Y%m")
#[1] "201601"
If we are going by the as.Date route, then 'Date' requires day also, so, paste a day and then use format after converting to 'Date'
format(as.Date(paste(str1, '01'), "%B %Y %d") , "%Y%m")
data
str1 <- "January 2016"

R difference between dates with different formats

What I'm trying to do is to calculate the total days between two dates. If the dates are in "yyyy/mm/dd" format I did it this way:
EndDate <- "2012/02/01"
StartDate <- "1900/01/01"
DiffBewtweenDates <- data.frame(date=c(EndDate),start=c(StartDate))
DiffBewtweenDates$date_diff <- as.Date(as.character(DiffBewtweenDates$date), format="%Y/%m/%d")-
as.Date(as.character(DiffBewtweenDates$start), format="%Y/%m/%d")
DiffBewtweenDates
And it worked. But I'm requested to get at least the EndDate in this format "FullDayName, DayNumber of FullMonthName of FullYearNumber". Something like this "Sunday, 1 of February of 2012".
As I understand by the R Manual, it would be ...format="%A, %d of %B of %Y"
But it doesn't work and I can't figure out why.
Thanks in advance for any idea.
Perhaps you got to change your locale to english:
Sys.setlocale("LC_TIME", "english")
date <- "Sunday, 1 of February of 2012"
lubridate::guess_formats(date, orders = "dmy")
# dmy
# "%A, %d of %B of %Y"
as.Date(date, guess_formats(date, orders = "dmy"))
# [1] "2012-02-01"
Anyway, you can use lubridate's guess_formats function to guess the formats for many date strings.
Simply to calculate difference in days and get desired output you can do
Sys.setlocale("LC_TIME", "C")
EndDate <- as.Date("2012/02/01", format = "%Y/%m/%d")
StartDate <- as.Date("1900/01/01", format = "%Y/%m/%d")
EndDate - StartDate
# Time difference of 40938 days
format(EndDate, "%A, %d of %B of %Y")
# [1] "Wednesday, 01 of February of 2012"

Change date format in R using as.Date

I am new to R and I am trying to change date format in the data frame for date columns. My date column is in format Mar 13 2007 01:05:123AM. Now this date format values are same except day change and time remains same. So I was thinking to change it to format as Mar 13 2007.
I tried the following code:
df <- read.csv("mydata.csv")
df$collectdate <- format(as.Date(df$collectdate,"%b %d %Y"))
but it gives error saying "character string is not in a standard unambiguous format". What can I try next?
You could try:
date <- "Mar 13 2007 01:05:123AM"
gsub("(.*)(?=\\s\\d{2}:).*", "\\1", date, perl=TRUE)
#[1] "Mar 13 2007"
For the as.Date, it didn't show any errors.
format(as.Date(date,"%b %d %Y"), "%b %d %Y")
#[1] "Mar 13 2007

Resources