# Today, I delved into the fascinating world of date manipulation in R, and I must say, I found it both enlightening and satisfying. I began by experimenting with the as.Date() function, which allowed me to convert various string representations into date objects seamlessly. I really enjoyed how flexible this function is, especially with its ability to parse dates in different formats.
#
# First, I worked on formatting dates. I took a date, December 15, 2023, and used the format() function to extract different components of the date. I found it quite handy that I could extract the abbreviated weekday, full weekday, abbreviated month, and full month with simple format specifiers like %a, %A, %b, and %B. It felt empowering to have such control over date presentation. For instance, I loved seeing "Fri" transform into "Friday" with just a tweak in the format.
#
# Next, I explored parsing strings into date objects. I discovered how R easily handles ISO 8601 date formats, but I found myself particularly intrigued by how it could interpret dates in non-standard formats. I played around with different date strings, like "07/14/23" and "April 5th, 2023", using specific format strings to guide R. I enjoyed the process of tweaking the formats until I got the correct date outputs. It gave me a sense of accomplishment knowing I could interpret and parse any date string thrown my way.
#
# I also took a closer look at coercing variables into date objects. I noticed how R treats these objects and how I could retrieve their class to confirm they were indeed of type "Date." This gave me confidence in my data manipulation, ensuring that I was working with proper date formats.
#
# One of the most interesting parts of my exploration was working with abbreviated and full month names. I loved the challenge of matching the format strings to the exact structure of the date strings I was parsing. For example, converting "5Dec2023" into a date object required precision, and I felt a sense of mastery as I successfully matched the format string %d%b%Y.
#
# In summary, my journey through date formatting and parsing in R today was an eye-opener. I learned to appreciate the nuances of handling dates, from formatting to parsing and even coercing strings into dates. This exercise made me realize how powerful and flexible R is when it comes to date manipulation. I am excited to continue my journey and apply these skills to real-world data projects.
# Formatting Dates
d = as.Date("2023-12-15") # Updated Date Time Stamp
format(d, "%a") # Abbreviated Weekday
## [1] "Fri"
format(d, "%A") # Full Weekday
## [1] "Friday"
format(d, "%b") # Abbreviated Month
## [1] "Dec"
format(d, "%B") # Full Month
## [1] "December"
format(d, "%m") # 00-12 Month Format
## [1] "12"
format(d, "%d") # 00-31 Day Format
## [1] "15"
format(d, "%e") # 0-31 Day Format
## [1] "15"
format(d, "%y") # 00-99 Year
## [1] "23"
format(d, "%Y") # Year with Century
## [1] "2023"
# Parsing Strings into Date Objects
as.Date('2023-12-01') # ISO format
## [1] "2023-12-01"
as.Date('07/14/23', format = '%m/%d/%y')
## [1] "2023-07-14"
as.Date('April 5th, 2023', '%B %dth, %Y')
## [1] "2023-04-05"
as.Date(' 2023-12-01 bar')
## [1] "2023-12-01"
as.Date(c('2023-05-01', '2023-05-02'))
## [1] "2023-05-01" "2023-05-02"
# Dates
x <- as.Date("2023-10-10")
x
## [1] "2023-10-10"
class(x)
## [1] "Date"
as.Date("10-10-2023", format="%d-%m-%Y") # European-style date
## [1] "2023-10-10"
as.Date("14/7/2023", format="%d/%m/%Y")
## [1] "2023-07-14"
as.Date("2023-10-10")
## [1] "2023-10-10"
as.Date("2023/10/10")
## [1] "2023-10-10"
# Using abbreviated and full month names
as.Date("5Dec2023", "%d%b%Y")
## [1] "2023-12-05"
as.Date("10 Dec, 2023", "%d %b, %Y")
## [1] "2023-12-10"
as.Date("February 20, 2023", "%B %d, %Y")
## [1] "2023-02-20"
as.Date("20 February, 2023", "%d %B, %Y")
## [1] "2023-02-20"
library(knitr)
library(kableExtra)
# Code snippets
code_snippets <- data.frame(
Step = c("Formatting Dates", "Parsing Strings", "Coercing to Date", "Abbreviated Month", "Full Month Name"),
Code = c(
"d = as.Date('2023-12-15')\nformat(d, '%a')\nformat(d, '%A')\nformat(d, '%b')\nformat(d, '%B')",
"as.Date('07/14/23', format = '%m/%d/%y')\nas.Date('April 5th, 2023', '%B %dth, %Y')",
"x <- as.Date('2023-10-10')\nclass(x)",
"as.Date('5Dec2023', '%d%b%Y')",
"as.Date('20 February, 2023', '%d %B, %Y')"
)
)
# Displaying the tablein colors
code_snippets %>%
kable("html", escape = FALSE, col.names = c("Step", "Code Snippet")) %>%
kable_styling(full_width = FALSE) %>%
row_spec(1, background = "#DFF0D8") %>% # Light green for first row
row_spec(2, background = "#D9EDF7") %>% # Light blue for second row
row_spec(3, background = "#F2DEDE") %>% # Light red for third row
row_spec(4, background = "#FCF8E3") %>% # Light yellow for fourth row
row_spec(5, background = "#EAEAEA") # Light grey for fifth row
|
Step
|
Code Snippet
|
|
Formatting Dates
|
d = as.Date(‘2023-12-15’) format(d, ‘%a’) format(d, ‘%A’) format(d,
‘%b’) format(d, ‘%B’)
|
|
Parsing Strings
|
as.Date(‘07/14/23’, format = ‘%m/%d/%y’) as.Date(‘April 5th, 2023’, ‘%B
%dth, %Y’)
|
|
Coercing to Date
|
x <- as.Date(‘2023-10-10’) class(x)
|
|
Abbreviated Month
|
as.Date(‘5Dec2023’, ‘%d%b%Y’)
|
|
Full Month Name
|
as.Date(‘20 February, 2023’, ‘%d %B, %Y’)
|