#Working with lubridate or date function####
pacman::p_load(pacman,tidyverse)
p_loaded(lubridate) #checks if a package is loaded
p_isinstalled(lubridate) #checks if the package is installed on the system or not
p_load(lubridate) #loads lubridate into the environment
#start work on Date function####
Sys.Date() #returns the present system date in yyyy-mm-dd format
d1 <- Sys.Date()
d1
class(d1) #d1 is date class
d2 <- "2021-08-06" #character class
class(d2)
d1
d2
d3 <- "06-08-2021"
d4 <- "06/08/2021"
d5 <- "06/08/21"
d6 <- "06 Aug, 2021"
d7 <- "Aug 06, 2021"
d8 <- "August 06, 2021"
d9 <- "6th August 2021"
d10 <- "06.08.2021"
d11 <- "08.06.2021"
d2 <- ymd(d2) #converts into date format; position of y,m,d should correspond with respective positions of year date and month in the initial format
class(d2)
d3 <- dmy(d3)
class(d3)
d4 <- dmy(d4)
d5 <- dmy(d5)
d6 <- dmy(d6)
d7 <- mdy(d7)
d8 <- mdy(d8)
d9 <- dmy(d9)
d10 <- dmy(d10)
d11<- mdy(d11)
dob <- "2002-05-09"
dob <- ymd(dob)
t1 <- today() #lubridate function that returns current date
#system date starts from 1900 1st Jan
t1 - dob #displays the difference between the two dates in -'number of days'
t2 <- t1 - dob #stored as difftime
weekdays(dob) #base function- returns day
week(dob) #lubridate funstion - returns week
yday(dob) #total number of days from the start of the year
month(dob)
year(dob) #isolates and displays the specified portion, eg: day(data) displays the day mentioned in data
day(dob)
leap_year(year(dob)) #logical output, true or false
t3 <- "1900-01-01"
t3 <- as.Date(t3) #alternate method to set data into date format
class(t3)
#playing with format command ####
dob <- as.character(dob)
d3 <- format(d3, format = "%d,%m,%Y") #reformats the variable from date format
#Refer to line 97 for more info about % in reformatting
origin #displays the start date in date format
d8 <- format(d8,format = "%B %d %Y") #you can change the separators while reformating
d9 <- format(d9, format = "%dth %B %Y") #use 'th' after %d if present in original format
class(d9)
d9 <- as.character(d9,format = "%dth %B %Y") #alternative to format()
d9
#data frame####
name <- c("Prateek","Swapan","Pooja")
dob <- c("2002-05-09","1950-08-05","1999-04-01")
gender <- c("Male","Male","Female")
marks <- c(100,400,500)
data <- data.frame(name,dob,gender,marks) #Creates data frame
View(data)
str(data)
data$dob <- ymd(data$dob) #setting dob to date format
data$gender <- as.factor(data$gender) #setting gender to factor
glimpse(data)
data$Year <- year(data$dob)
data$Month <- month(data$dob)
data$Day <- day(data$dob)
data$day_of_the_week <- weekdays(data$dob)
data$Age <- as.integer((t1 - data$dob)/365)
#date format Symbol Meaning Example =====
# %d day as a number (0-31) 01-31
# %a abbreviated weekday Mon
# %A unabbreviated weekday Monday
# %m month (00-12) 00-12
# %b abbreviated month Jan
# %B unabbreviated month January
# %y 2-digit year 07
# %Y 4-digit year 2007