Manipulate Date
a. Use package zoo
Assume in original dat1, field “Date” is: “Apr-2017” with type(Date) is: character. So, we need to convert into Date type. The reason we need to convert to Date type or yearmon type is because Date can be arranged in order
Use package (zoo)
Eg: dat1.Date = as.yearmon(dat1.Date, “%b-%Y”)
Notice: we have to write the same format as in dat1 (%b-Y) in function
b. Use package lubridate
(Source: http://rstudio-pubs-static.s3.amazonaws.com/268221_9a8a8794a0384c8e9acf9020869adbae.html)
#install.packages("lubridate")
library(lubridate)
#install.packages("dplyr")
library(dplyr)
#install.packages("hflights")
library(hflights)
str(hflights)
## 'data.frame': 227496 obs. of 21 variables:
## $ Year : int 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 ...
## $ Month : int 1 1 1 1 1 1 1 1 1 1 ...
## $ DayofMonth : int 1 2 3 4 5 6 7 8 9 10 ...
## $ DayOfWeek : int 6 7 1 2 3 4 5 6 7 1 ...
## $ DepTime : int 1400 1401 1352 1403 1405 1359 1359 1355 1443 1443 ...
## $ ArrTime : int 1500 1501 1502 1513 1507 1503 1509 1454 1554 1553 ...
## $ UniqueCarrier : chr "AA" "AA" "AA" "AA" ...
## $ FlightNum : int 428 428 428 428 428 428 428 428 428 428 ...
## $ TailNum : chr "N576AA" "N557AA" "N541AA" "N403AA" ...
## $ ActualElapsedTime: int 60 60 70 70 62 64 70 59 71 70 ...
## $ AirTime : int 40 45 48 39 44 45 43 40 41 45 ...
## $ ArrDelay : int -10 -9 -8 3 -3 -7 -1 -16 44 43 ...
## $ DepDelay : int 0 1 -8 3 5 -1 -1 -5 43 43 ...
## $ Origin : chr "IAH" "IAH" "IAH" "IAH" ...
## $ Dest : chr "DFW" "DFW" "DFW" "DFW" ...
## $ Distance : int 224 224 224 224 224 224 224 224 224 224 ...
## $ TaxiIn : int 7 6 5 9 9 6 12 7 8 6 ...
## $ TaxiOut : int 13 9 17 22 9 13 15 12 22 19 ...
## $ Cancelled : int 0 0 0 0 0 0 0 0 0 0 ...
## $ CancellationCode : chr "" "" "" "" ...
## $ Diverted : int 0 0 0 0 0 0 0 0 0 0 ...
hflights <- mutate(hflights, Date = paste(Year, Month, DayofMonth, sep = "-"))
str(hflights$Date)
## chr [1:227496] "2011-1-1" "2011-1-2" "2011-1-3" "2011-1-4" "2011-1-5" ...
#1st way: using package zoo
#install.packages("zoo")
library(zoo)
hflights$Date <- as.yearmon(hflights$Date, "%Y-%m-%d")
class(hflights$Date)
## [1] "yearmon"
#2nd way: using lubridate
hflights$Date <- ydm(hflights$Date)
## Warning: All formats failed to parse. No formats found.
class(hflights$Date)
## [1] "Date"