Titanic Ad For Journey From New York To Europe
<-
is the assignment operatorhi
has a length of onehi
is assigned the value, Hello World
hi <- "Hello World"
hi
[1] "Hello World"
<-
was the first assignment operator=
works, <-
is the standard
## These are equivalent.
a <- 1
b = 1
John Jacob Astor in 1909. Richest man to die aboard the Titanic
<-
and =
assign variables in the local environment<<-
run away!
<<-
is the global assignment operator
## In this session:
a ## This works.
A ## - This does not.
R calls this the data's "class"
This is more of an FYI than anything else
?numeric
FLOAT
FLOAT
NUM
## The passenger capacity of the Titanic.
passenger_capacity <- 2435
## The crew capacity of the Titanic.
crew_capacity <- 892
+
-
*
/
^
total_capacity <- passenger_capacity + crew_capacity
percent_crew = 100 * crew_capacity / total_capacity
percent_crew
[1] 26.81094
## Another way to do it!
total_capacity <- sum(passenger_capacity, crew_capacity)
total_capacity
[1] 3327
is.numeric(total_capacity)
Returns:
[1] TRUE
is.numeric(hi)
Returns:
[1] FALSE
Compare the results. Does this make sense?
total_capacity
class(total_capacity)
percent_crew
class(percent_crew)
(The answers are on the next slide!)
> total_capacity
[1] 3327
> class(total_capacity)
[1] "numeric"
> percent_crew
[1] 26.81094
> class(percent_crew)
[1] "numeric"
'foo'
or "foo"
<-
)string
CHAR
char()
, varchar()
## Let's make some character variables!
title <- "Captain"
name <- "Smith"
## And do something . . . cool . . . with them.
paste(title, name, sep=" ")
[1] "Captain Smith"
TRUE
FALSE
True, False
BOOL
or INT
(There may or may not be an analogue)
survived <- TRUE
## OR
survived <- FALSE
- Note: CAPITALIZATION MATTERS!
==
=
, is an assignment operator)<
, <=
>
, >=
Boolean Operators ALWAYS Return: TRUE
, FALSE
OR NA
Symbol | Mnemonic |
< |
Lt |
<= |
Le |
> |
Gt |
>= |
Ge |
= |
Eq |
~= |
Ne |
# Were there more passengers than crew?
> passenger_capacity > crew_capacity
[1] TRUE
# Were there exactly 1,000 passengers?
> passenger_capacity == 1000
[1] FALSE
## Assign the results to a variable.
answer <- passenger_capacity < 1000
## Doing so returns nothing, unless there is an error.
## Captain Edward Smith was 62 years old when he died.
## Was he older when he died than you are today?
answer <- 62 > ?
answer
## Additional Questions:
## Is 'answer' a Numeric?
## Is 'answer' a Boolean?
as.Date()
D
?
## POSIX Compatible dates:
## You can write it as a mm/dd/yyyy, but . . .
## then you have to tell R how to read the date.
capt_smith_dob <- as.Date("1850-01-27")
## I won't make you practice these today.
>
+
instead
round(9.9
+
## What does this do?
## And how is this different?
c(1,2,3)
Titanic in Cobh Harbour, County Cork Ireland