In this homework we are taking character and numeric values and converting them into dates. We are also taking dates and coverting them to other date formats.
Write an R function that converts a date of the format “yyyymmdd” such as “20200325” to a date of the format “mm/dd/yyyy”. Your function should be able to handle a vector input. For example, if your input is c(20200312, 20201023, 20201225), the output should be a vector of with elements 3/12/2020 and 12/25/2020.
## Chaning Character to Date Format
fun1 <- function(a,b,c){
as.Date(c(a,b,c),"%Y%m%d")
}
fun1("20200312", "20201023", "20201225")## [1] "2020-03-12" "2020-10-23" "2020-12-25"
Write an R function that converts a date of the format “mm/dd/yyyy” such as “09/15/2020” or “9/15/2020” to a date of the format “month day yyyy” such as “Sep 15 2020”. It should be able to handle a vector input correctly.
fun2 <- function(a,b,c){
out <- (c(a,b,c))
out <- str_replace_all(out,"/","")
out <- as.Date(out,"%m%d%Y")
out1<-format(out,"%B %d %Y")
out2<-format(out,"%A, %B %d, %Y")
out3<-format(out,"%j - %Y")
out4<-format(out,"%u %Y")
print(out1)
print(out2)
print(out3)
print(out4)
}
fun2("09/15/2020","09/16/2020","09/17/2020")## [1] "September 15 2020" "September 16 2020" "September 17 2020"
## [1] "Tuesday, September 15, 2020" "Wednesday, September 16, 2020"
## [3] "Thursday, September 17, 2020"
## [1] "259 - 2020" "260 - 2020" "261 - 2020"
## [1] "2 2020" "3 2020" "4 2020"