How to handle Date in format Like "Wednesday-September 7-2011" - r

I am trying to get Date column from an excel data. The format of date in excel is like Wednesday-September 7-2011.
How do I handle dates in such format? I've read the documentation on Date and cannot find any method.

as.Date("Wednesday-September 7-2011", "%A-%B %d-%Y")
# [1] "2011-09-07"
https://www.stat.berkeley.edu/~s133/dates.html

If all your dates follow the same format, then I 'd suggest to remove the day and parse the rest, i.e.
x <- 'Wednesday - September 7 - 2011'
y <- paste(strsplit(x, ' - ')[[1]][-1], collapse = ' ')
#which gives [1] "September 7 2011"
as.POSIXct(y, format = '%B %d %Y')
#[1] "2011-09-07 EEST"

I'd probably strip out the weekday name and then parse the rest of the date. For example:
x <- "Wednesday-September 7-2011"
pos <- regexpr("-", x)
y<- (substr(x,pos+1,nchar(x)))
z<- parse_date(y, format = "%B %d-%Y")

Related

Error by changing all the date format in R

I'm trying to change the format from all the dates in one column. I tried it with strftime, as.Date and with parse_date, all giving me some sort of issue. The column includes 2200 different times currently expressed in the following format: Feb-03-2022, it should be expressed as: "%B %d %Y", how could I modify all dates?
ethereum <- read_csv('ethereum_2022-01-04_2022-02-03.csv')
head(ethereum)
# Changing date format in the dataset
ethereum$Date <- parse_date(ethereum$`Date`, "%d-%b-%y")
head(ethereum$Date)
# Naming the datatype and the timeseries
ds<- ethereum$Date
y<- ethereum$`Close`
df<- data.frame(ds,y)
View(df)
When I try with this code, I get the following error:
Warning: 2200 parsing failures.
row col expected actual
1 -- date like %d-%b-%y Feb-03-2022
2 -- date like %d-%b-%y Feb-02-2022
3 -- date like %d-%b-%y Feb-01-2022
4 -- date like %d-%b-%y Jan-31-2022
5 -- date like %d-%b-%y Jan-30-2022
... ... .................. ...........
See problems(...) for more details.
You can convert the vector to the date format and then apply any desired formatting.
vec_str <- c("Feb-03-2022", "Feb-02-2022", "Jan-31-2022", "Jan-30-2022")
vec_dates <- as.Date(x = vec_str, format = "%b-%d-%Y")
vec_dates_str <- format(vec_dates, "%B %d %y")
vec_dates_str
# [1] "February 03 22" "February 02 22" "January 31 22" "January 30 22"
For convenience of applying in data frame you can wrap this behaviour in a function:
my_date_transform <- function(x,date_in_format = "%b-%d-%Y",
date_out_format = "%B-%d-%Y") {
x_dates <- as.Date(x = x, format = date_in_format)
format(x = vec_dates, date_out_format)
}
my_date_transform(x = vec_str)
Example
sample_data <- data.frame(original_date_str = vec_str)
sample_data$new_date_format <- my_date_transform(sample_data$original_date_str)
sample_data
# >> sample_data
# original_date_str new_date_format
# 1 Feb-03-2022 February-03-2022
# 2 Feb-02-2022 February-02-2022
# 3 Jan-31-2022 January-31-2022
# 4 Jan-30-2022 January-30-2022
You can then apply your function to a data frame
I assume you are having a packages problem.
Try this example:
library(parsedate)
## Calling the function
parse_date("Feb-03-2022")
## Specifying the package to avoid masked functions
parsedate::parse_date("Feb-03-2022")
In your code would look like this:
ethereum$Date <- parse_date(ethereum$Date)
The package "parsedate", is used to parse from any date format. I don't know if all the 2.2k are in the same format, so my suggestion is to use that. In case you are 100% sure, you can use many other parsing date functions. You can even write one yourself using string processing techniques.

Custom Date Transformation in R

Using R
How do we convert "yyyymm" to "yyyy-mm-01" across all the rows?
Eg: "201603" to "2016-03-01" (ie "yyyy-mm-dd" format)
PS: Here, (dd = 01) is the default date for all 12 months. ie("2016-01-01" , "2016-02-01" , etc...)
A simple paste solution:
x <- "201603"
paste0(substr(x, 1,4), "-", substr(x, 5,6), "-01")
[1] "2016-03-01"
If you want to transform as date:
as.Date(paste0(201603, 01), format = "%Y%m%d")
This will create the 2016-03-01 format as the date and not as a character.
If you want to use in all rows on column Date
data <- data %>%
mutate(Date = as.Date(paste(Date, 01) format = "%Y%m%d")
additional solution
library(lubridate)
library(stringr)
x <- c("201603")
ymd(str_c(x,"01"))
[1] "2016-03-01"

Convert character YYYY-MM-00 into date YYYY-MM in R

I imported Excel data into R and I have a problem to convert dates.
In R, my data are character and look like :
date<-c('1971-02-00 00:00:00', '1979-06-00 00:00:00')
I would like to convert character into date (MM/YYYY) but the '00' value used for days poses a problem and 'NA' are returned systematically.
It works when I manually replace '00' with '01' and then use as.yearmon, ymd and format. But I have lots of dates to change and I don't know how to change all my '00' into '01' in R.
# data exemple
date1<-c('1971-02-00 00:00:00', '1979-06-00 00:00:00')
# removing time -> doesn't work because of the '00' day
date1c<-format(strptime(date1, format = "%Y-%m-%d"), "%Y/%m/%d")
date1c<-format(strptime(date1, format = '%Y-%m'), '%Y/%m')
# trying to convert character into date -> doesn't work either
date1c<-ymd(date1)
date1c<-strptime(date1, format = "%Y-%m-%d %H:%M:%S")
date1c<-as.Date(date1, format="%Y-%m-%d %H:%M:%S")
date1c<as.yearmon(date1, format='%Y%m')
# everything works if days are '01'
date2<-c('1971-02-01 00:00:00', '1979-06-01 00:00:00')
date2c<-as.yearmon(ymd(format(strptime(date2, format = "%Y-%m-%d"), "%Y/%m/%d")))
date2c
If you have an idea to do it or an another idea to solve my problem, I would be thankful!
Use gsub to replace -00 with -01.
date1<-c('1971-02-01 00:00:00', '1979-06-01 00:00:00')
date1 <- gsub("-00", "-01", date1)
date1c <-format(strptime(date1, format = "%Y-%m-%d"), "%Y/%m/%d")
> date1c
[1] "1971/02/01" "1979/06/01"
Another possibility could be:
as.Date(paste0(substr(date1, 1, 9), "1"), format = "%Y-%m-%d")
[1] "1971-02-01" "1979-06-01"
Here it extracts the first nine characters, pastes it together with 1 and then converts it into a date object.
These alternatives each accept a vector input and produce a vector as output.
Date output
These all will accept a vector as input and produce a Date vector as the output.
# 1. replace first occurrence of '00 ' with '01 ' and then convert to Date
as.Date(sub("00 ", "01 ", date1))
## [1] "1971-02-01" "1979-06-01"
# 2. convert to yearmon class and then to Date
library(zoo)
as.Date(as.yearmon(date1, "%Y-%m"))
## [1] "1971-02-01" "1979-06-01"
# 3. insert a 1 and then convert to Date
as.Date(paste(1, date1), "%d %Y-%m")
## [1] "1971-02-01" "1979-06-01"
yearmon output
Note that if you really are trying to represent just months and years then yearmon class directly represents such objects without the kludge of using an unused day of the month. Such objects are internally represented as a year plus a fraction of a year, i.e. year + 0 for January, year + 1/12 for February, etc. They display in a meaningful way, they sort in the expected manner and can be manipulated, e.g. take the difference between two such objects or add 1/12 to get the next month, etc. As with the others it takes a vector in and produces a vector out.
library(zoo)
as.yearmon(date1, "%Y-%m")
## [1] "Feb 1971" "Jun 1979"
character output
If you want character output rather than Date or yearmon output then these variations work and again accept a vector as input and produce a vector as output:
# 1. replace -00 and everything after that with a string having 0 characters
sub("-00.*", "", date1)
## [1] "1971-02" "1979-06"
# 2. convert to yearmon and then format that
library(zoo)
format(as.yearmon(date1, "%Y-%m"), "%Y-%m")
## [1] "1971-02" "1979-06"
# 3. convert to Date class and then format that
format(as.Date(paste(1, date1), "%d %Y-%m"), "%Y-%m")
## [1] "1971-02" "1979-06"
# 4. pick off the first 7 characters
substring(date1, 1, 7)
## [1] "1971-02" "1979-06"

Convert dates to text in R

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"

Separate Date into week and year

Currently my dataframe has dates displayed in the 'Date' column as 01/01/2007 etc I would like to convert these into a week/year value i.e. 01/2007. Any ideas?
I have been trying things like this and getting no where...
enviro$Week <- strptime(enviro$Date, format= "%W/%Y")
You have to first convert to date, then you can convert back to the week of the year using format, for example:
### Converts character to date
test.date <- as.Date("10/10/2014", format="%m/%d/%Y")
### Extracts only Week of the year and year
format(test.date, format="Week number %W of %Y")
[1] "Week number 40 of 2014"
### Or if you prefer
format(date, format="%W/%Y")
[1] "40/2014"
So, in your case, you would do something like this:
enviro$Week <- format(as.Date(enviro$Date, format="%m/%d/%Y"), format= "%W/%Y")
But remember that the part as.Date(enviro$Date, format="%m/%d/%Y") is only necessary if your data is not in Date format, and you also should put the right format parameter to convert your character to Date, if that is the case.
What is the class of enviro$Date? If it is of class Date there is probably a better way of doing this, otherwise you can try
v <- strsplit(as.character(enviro$Date), split = "/")
weeks <- sapply(v, "[", 2)
years <- sapply(v, "[", 3)
enviro$Week <- paste(weeks, years, sep = "/")

Resources