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.
CodePudding user response:
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.
CodePudding user response:
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