Course Notes
Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! The datasets used in this course are available in the datasets folder.
# Import any packages you want to use here
Take Notes
Add notes here about the concepts you've learned and code cells with code you want to keep.
There are a number of different formats you can specify, here are a few of them:
%Y: 4-digit year (1982) %y: 2-digit year (82) %m: 2-digit month (01) %d: 2-digit day of the month (13) %A: weekday (Wednesday) %a: abbreviated weekday (Wed) %B: month (January) %b: abbreviated month (Jan)
Add your notes here
different types of relational operators. For reference, here they are again:
: Greater than =: Greater than or equal to < : Less than <=: Less than or equal to ==: Equality !=: Not equal
& (And): An intersection. a & b is true only if both a and b are true. | (Or): A union. a | b is true if either a or b is true.
# Add your code snippets here
Dates# What is the current date?
Sys.Date ()
# What is the current date and time?
Sys.time()
# Create the variable today
today <- Sys.Date()
# Confirm the class of today
class(today)
# Create crash
crash <- as.Date("2008-09-29")
# Print crash
crash
# crash as a numeric
as.numeric(crash)
# Current time as a numeric
as.numeric(Sys.time())
## the date is given in the format of "yyyy-mm-dd". This is known as ISO format (ISO = International Organization for Standardization), and is the way R accepts and displays dates.
# Create dates from "2017-02-05" to "2017-02-08" inclusive
dates <- c("2017-02-05", "2017-02-06", "2017-02-07","2017-02-08" )
# Add names to dates
names(dates) <- c("Sunday", "Monday", "Tuesday", "Wednesday")
# Subset dates to only return the date for Monday
dates[names(dates) == "Monday"]
as.Date("09/28/2008", format = "%m / %d / %Y")
[1] "2008-09-29"
# "08,30,30"
as.Date("08,30,1930", format = "%m, %d, %Y")
# "Aug 30,1930"
as.Date("Aug 30,1930", format = "%b %d, %Y")
# "30aug1930"
as.Date("30aug1930", format = "%d%b%Y")
char_dates <- c("1jan17", "2jan17", "3jan17", "4jan17", "5jan17")
# Create dates using as.Date() and the correct format
dates <- as.Date(char_dates, format = "%d%b%y")
dates
# Use format() to go from "2017-01-04" -> "Jan 04, 17"
format(dates, format = "%b %d, %y")
# Use format() to go from "2017-01-04" -> "01,04,2017"
format(dates, format = "%m, %d, %Y")today <- as.Date("2017-01-02")
tomorrow <- as.Date("2017-01-03")
one_year_away <- as.Date("2018-01-02")
tomorrow - today
Time difference of 1 days
one_year_away - today
Time difference of 365 daysdifftime(tomorrow, today)
Time difference of 1 days
# With some extra options!
difftime(tomorrow, today, units = "secs")
Time difference of 86400 secs# Dates
dates <- as.Date(c("2017-01-01", "2017-01-02", "2017-01-03"))
# Create the origin
origin <- as.Date("1970-01-01")
# Use as.numeric() on dates
as.numeric(dates)
# Find the difference between dates and origin
dates - origin# dates
dates <- as.Date(c("2017-01-02", "2017-05-03", "2017-08-04", "2017-10-17"))
# Extract the months
months(dates)
# Extract the quarters
quarters(dates)
# dates2
dates2 <- as.Date(c("2017-01-02", "2017-01-03", "2017-01-04", "2017-01-05"))
# Assign the weekdays() of dates2 as the names()
names(dates2) <- weekdays(dates2)
# Print dates2
dates2# Print stocks
stocks
# IBM range
stocks$ibm_buy <- stocks$ibm < 175
# Panera range
stocks$panera_sell <- stocks$panera > 213
# IBM vs Panera
stocks$ibm_vs_panera <- stocks$ibm > stocks$panera
# Print stocks
stocks