'R Financial Year Date Conversion
Quick question, is is possible to convert the below character fields to allow me to use them in a trend chart. Thes fields represent financial quarters.
"Jul - Sep 2020", "Jul - Sep 2021", "Oct - Dec 2020", "Apr - Jun 2021", "Jan - Mar 2021", "Oct - Dec 2021"
"Jan - Mar 2022"
Thanks
Solution 1:[1]
As pointed out in the comments you need to make an assumption on which date of the quarter to take as a reference.
Please, see if the example below is what you need.
# Original date
x <- c("Jul - Sep 2020", "Jul - Sep 2021", "Oct - Dec 2020",
"Apr - Jun 2021", "Jan - Mar 2021", "Oct - Dec 2021")
# Split all elements of the date
df <- data.frame(matrix(unlist(
strsplit(as.character(x), split = " ")), ncol = 4, byrow = TRUE)
)
# Convert to date format (assuming you take the first day of the quarter)
df$original_date <- x
df$modified_date <- paste0("1", tolower(df$X1), df$X4)
df$final_date <- as.Date(df$modified_date, format = "%d%b%Y")
# Add column of the value I wont to plot
df$myvalue <- 1:6
# Order the table
df <- df[order(df$final_date),]
str(df)
# Plot labelling date with original format
plot(x = df$final_date, y = df$myvalue, type = "b",
at = df$final_date, labels = df$original_date)
The plot looks like this:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sara |