Working with Dates

 d1 <- date(); d1 #will render a character variable of the date & time
## [1] "Wed Jul 29 18:28:43 2020"
# [1] "Wed Jul 29 15:48:19 2020"
 
 d2 <-Sys.Date(); d2 #will render a date variable of the date
## [1] "2020-07-29"
#[1] "2020-07-29"
 
format(d2, "%a, %b, %d") #will render a 'pretty' text
## [1] "Wed, Jul, 29"
#[1] "Wed, Jul, 29"

x = c("23Mar20", "9Aug2015", "29Feb19"); z = as.Date(x,"%d%b%Y")
z #transforms the characters to a date, if it exists
## [1] "0020-03-23" "2015-08-09" NA
#[1] "0020-03-23" "2015-08-09" NA 

# which can then be used to do calculations
z[2]-z[1]
## Time difference of 728797 days
as.numeric(z[2]-z[1])
## [1] 728797
julian(d2)
## [1] 18472
## attr(,"origin")
## [1] "1970-01-01"
weekdays(d2); months(d2)
## [1] "Wednesday"
## [1] "July"
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
# searches & converts to date type, in UCT
ymd("20200525")
## [1] "2020-05-25"
mdy("02/20/1525")
## [1] "1525-02-20"
dmy("02/12/2525")
## [1] "2525-12-02"
# searches & converts to date type, in UCT
dmy_hms("31/12/2020 23:59:59 ")
## [1] "2020-12-31 23:59:59 UTC"
#set the time zone
dmy_hms("31/12/2020 23:59:59", tz= "Pacific/Auckland")
## [1] "2020-12-31 23:59:59 NZDT"
#to find the weekday
x = dmy(c("23Mar20", "9Aug2015"))
wday(x[1])
## [1] 2
#to get the "pretty" form
wday(x[1], label = TRUE)
## [1] Mon
## Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
#?POSIXlt()
#Open Government Data

#http://www.data.gov/ USA
#http://www.data.gouv.fr/ France
#http://data.gov.uk/ UK

#Other sources:

#http://www.gapminder.org/ Public Health
#http://www.asdfree.com/ US surveys data
#http://www.infochimps.com/marketplace
#http://www.kaggle.com/

#Famous Data Scientists

#http://bitly.com/bundles/hmason/1 Hilary Mason's research data
#http://snap.stanford.edu/data/ Stanford Large Network Dataset Collection
#http://archive.ics.uci.edu/ml/ UCI Machine Learning Repository
#http://www.kdnuggets.com/datasets/index.html Datasets for Data 

#Mining and Data Science
#http://lib.stat.cmu.edu/datasets/
#http://www.ncbi.nlm.nih.gov/geo/ a public functional genomics data repository
#http://arxiv.org/help/bulk_data - data from arxiv

#R Packages
#https://ropensci.org/packages/

Week 4 Course Notes: data-cleaning in R, by [Linda] (@lindangulopez)