Hello World

  • Run code, learn more
  • Duration: ~45 Minutes
  • Followed By: A Break!
Photo of the Titanic

Learning Objectives

  • Create Variables
  • Data Types
  • Yachting For Beginners

Titanic Ad For Journey From New York To Europe

Hello World

  • <- is the assignment operator
  • Variable hi has a length of one
  • Variable hi is assigned the value, Hello World
  • Everything in R, including this variable, is an object
  • It is easy to return the assigned value of a variable

hi <- "Hello World"
hi
    

[1] "Hello World"    
    

Assignment Conventions


## These are equivalent.
a <- 1
b =  1
    

Why Be Conventional?

  • All languages have conventions
  • KNOW them, to read demo code, etc
  • Only deviate if you have good reason to do so
  • TITR uses Base R conventions, unless otherwise noted
Photo of John Jacob Astor

John Jacob Astor in 1909. Richest man to die aboard the Titanic

Assignment Conventions

Checking For Understanding

Text Editor v REPL

Second Screenshot of RStudio

Capitalization Matters

  • SAS & SQL ARE NOT case sensitive
  • Most languages such as R and Python ARE case sensitive

## In this session:

a ## This works.

A ## - This does not.
        

Data Types
Introduced Today

R calls this the data's "class"

Our Focus:
  • Numeric
  • Character
  • Logical (Boolean)
Additional:
  • Date
  • Factor (Later today)

Data Types
Not Introduced Today

This is more of an FYI than anything else

  • Integer
  • Complex
  • Raw

Numeric

R:
  • These are numbers!
  • For more info: ?numeric
Analogues:
  • Python: FLOAT
  • SQL: FLOAT
  • SAS: NUM

Numeric


## The passenger capacity of the Titanic.
passenger_capacity <- 2435

## The crew capacity of the Titanic.
crew_capacity <- 892
    
  • Note: Do not use a comma separator in a numeric

Numeric

  • Arithmetic is straightforward
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Exponentiation: ^

Photo of Captain Smith Your Turn!

  • Can you calculate the total capacity of the Titanic?
  • What percentage of the total capacity were the staff?

total_capacity <- passenger_capacity + crew_capacity

percent_crew = 100 * crew_capacity / total_capacity

percent_crew
    
[1] 26.81094

Functions

  • We use functions to alter or understand our data
  • Arithmetic operators are actually special functions

## Another way to do it!
total_capacity <- sum(passenger_capacity, crew_capacity)
total_capacity
    

[1] 3327
    

Is Numeric?

  • Is a variable numeric?
  • is.numeric() returns a Boolean
is.numeric(total_capacity)

Returns:

[1] TRUE

is.numeric(hi)

Returns:

[1] FALSE

Photo of Captain Smith Your Turn!

Compare the results. Does this make sense?


total_capacity

class(total_capacity)
            

percent_crew

class(percent_crew)
            

(The answers are on the next slide!)

Photo of Captain Smith Your Turn!


> total_capacity
[1] 3327

> class(total_capacity)
[1] "numeric"
    
            
        

> percent_crew
[1] 26.81094

> class(percent_crew)
[1] "numeric"
            

Characters

R:
  • Characters can be of any arbitrary length
    (memory dependent)
  • Quotes: 'foo' or "foo"
    • Double quotes are the convention (mostly)
  • Assignment uses (<-)
Analogues:
  • Python: string
  • SAS: CHAR
  • SQL: char(), varchar()

Photo of Captain Smith Your Turn!


## Let's make some character variables!
title <- "Captain"
name  <- "Smith"

## And do something . . . cool . . .  with them.
paste(title, name, sep=" ")
    

[1] "Captain Smith"
    

Booleans

In R:
  • TRUE
  • FALSE
  • Assignment works the way you would expect
Analogues:
  • Python: True, False
  • SQL: Either BOOL or INT (There may or may not be an analogue)
  • SAS: 1 = Yes, 0 = No.
    No direct analogue in SAS

Booleans


survived <- TRUE

## OR

survived <- FALSE
    
- Note: CAPITALIZATION MATTERS!

Comparison Operators

In R:
  • Equals: ==
    (Remember, =, is an assignment operator)
  • Less Than: <, <=
  • Greater Than:>, >=
  • Not Equal: !=

Boolean Operators ALWAYS Return: TRUE, FALSE OR NA

In Python/SQL
  • Identical to R


In SAS:
Symbol Mnemonic
< Lt
<= Le
> Gt
>= Ge
= Eq
~= Ne

Comparisons


# 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.
    

Photo of Captain Smith Your Turn!


## 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?

    

Dates

  • Not needed today, just demonstrative
  • Fact of Life: Dates universally suck
  • R is no different
  • Like Integers, must be explicitly declared
  • as.Date()
    • Why the capital D?
    • Dunno

Dates


## 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.
    

REPL Gotcha!

  • Each line in R's REPL usually starts with: >
  • Sometimes a line starts with a + instead
  • This is R waiting for you to finish a command
  • You failed to close a pair of parens, quotation, etc.

round(9.9
+ 
    

Photo of Captain Smith Your Turn!


## What does this do?
## And how is this different?
c(1,2,3)
    

Take A Break!

Titanic in Cobh Harbour, County Cork Ireland

Titanic in Cobh Harbour, County Cork Ireland