2  2. variables, integer division and remainder operators

2.1 %/% for integer division     %% for remainder (AKA modulus)

# Symbols such as +, -, *, /, ^ and (parentheses) are known as "operators"
# since they perform "operations". For example, the + performs the "addition operation"
# and "*" performs the "multiplication operation".

# R has additional operators that go beyond regular math. For example
# Some operators consist of two percent signs with possibly something in between ...

# Integer division is done using %/%. This gives the whole number result of dividing a
# number by another number. Any numbers after a decimal point are removed.
13 %/% 4  # answer: 3
[1] 3
# Remainder (also known as, AKA, "modulus") is done using just two %% signs. This is the
# "remainder" of dividing the first number by the 2nd number. For example, 15 %% 4, is 1
# since 4 goes into 15 3 times with 1 left over (3*4 is only 12, but 13 is 1 more than that)
13 %% 4   # answer: 1
[1] 1
# remember 13/4 is regular division, i.e. 3.25
13/4
[1] 3.25

2.2 ———- PRACTICE ———-

# Use the following variables while answering the questions below.
#
# Your answers to the questions should work even if these variables 
# would have been assigned different values (i.e. different numbers).

numberOfCandies = 27
numberOfChildren = 5
#-----------------------------------------------------------------------------.
# QUESTION: 
# 
# Write R code to figure out the following ...
#
# If there are numberOfCandies, pieces of candy in a package and numberOfChildren
# children, what is the maximum number of
# candies we can give each child so that all the children get the same number of candies?
#-----------------------------------------------------------------------------.
numberOfCandies %/% numberOfChildren  
[1] 5
#-----------------------------------------------------------------------------.
# QUESTION: 
#
# (see the information in the previous question)
# How many candies will be left over after we give out the candies to the children?
#-----------------------------------------------------------------------------.
numberOfCandies %% numberOfChildren   
[1] 2

2.3 A variable is a name for a value

# You can use "variables" to represent values in R.
# A variable has a "variable name" and a "value". For example, suppose Joe's salary
# is 50 thousand dollars. You can represent that as follows:

joesSalary = 50000    # notice that "Salary" has a capital "S" to make it easy to read.

# To see the value of joesSalary, you can simply type the word joesSalary
joesSalary
[1] 50000

2.4 assignment statments

# The line above, "joesSalary=50000", is known as an "assignment statement". It assigns
# a value to a variable name. The left hand side of the "=" sign is the name of 
# the variable. 
#
# The "name" of the variable (i.e. joesSalary) is on the left hand side of the = sign and 
# the "value" of the variable (i.e. 50000) is on the right hand side of the = sign.

2.5 Do NOT include spaces in a variable name

# Do NOT include spaces in variable names!
# You can separate between words in a variable name in a few different ways:

# camelCase - i.e. capitalize the first letter of each word (except maybe the 1st word).
# For example:
jackSmithsSalary = 70

# use periods or underscores between the words. For example
sue.cohens.salary = 80
anne_johnsons_salary = 90

# All of the above methods are "correct" and valid. 
# However, it is best to be consistent and stick to one method for all your code.
# In this book, I will generally use camelCase as it is 1 character less to type.

2.6 The right hand side could be any “expression”.

# Let's start with a variable to record the price of a pineapple.
priceOfApple = 1.99

# The right hand side could be any "expression". For example:
#
# Suppose a person buys 5 apples and hands the cashier a $20 bill. Write a command
# that assigns the amount of change the person gets back into the variable change.

change = 20 - priceOfApple * 5

# Show the change
change
[1] 10.05

2.7 more rules for variable names

Variable names may NOT start with a number

oneOrange = 1.50    # good

# 1Banana = 1.25      # ERROR

price1 = 50.00    # good

Variable names may ONLY include letters, numbers, underscores “_” and periods “.”

this.is.a.good.variable.name = 100

this_is_also_a_good_variable_name = 200

# this$is$not = 300    # ERROR - bad variable name

Variable names are case sensitive.

# The following are TWO DIFFERENT VARIBLES
Lettuce = 0.99
lettuce = 3.50

# Show the values of each variable
Lettuce
[1] 0.99
lettuce
[1] 3.5

2.8 Managing your variables:

Environment window

You can see the values of all variables in the “Environment” window in R Studio By default, the “Environment” window is in the upper right hand corner of your screen.

ls() # lists the defined variables

To see the names of all your existing variables you can run the ls() command.
NOTE - you MUST include the (parentheses) - see below.

ls()    # you MUST include the (parentheses)
 [1] "anne_johnsons_salary"              "change"                           
 [3] "jackSmithsSalary"                  "joesSalary"                       
 [5] "lettuce"                           "Lettuce"                          
 [7] "numberOfCandies"                   "numberOfChildren"                 
 [9] "oneOrange"                         "price1"                           
[11] "priceOfApple"                      "sue.cohens.salary"                
[13] "this.is.a.good.variable.name"      "this_is_also_a_good_variable_name"

Don’t forget the parentheses! ls() NOT ls

If you forget to type the parentheses, you will see a bunch of stuff that is way beyond the scope of what we are talking about today (we’ll get to that later in the course)

# This is what you see if you forget the (parentheses) after ls().
ls 
function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE, 
    pattern, sorted = TRUE) 
{
    if (!missing(name)) {
        pos <- tryCatch(name, error = function(e) e)
        if (inherits(pos, "error")) {
            name <- substitute(name)
            if (!is.character(name)) 
                name <- deparse(name)
            warning(gettextf("%s converted to character string", 
                sQuote(name)), domain = NA)
            pos <- name
        }
    }
    all.names <- .Internal(ls(envir, all.names, sorted))
    if (!missing(pattern)) {
        if ((ll <- length(grep("[", pattern, fixed = TRUE))) && 
            ll != length(grep("]", pattern, fixed = TRUE))) {
            if (pattern == "[") {
                pattern <- "\\["
                warning("replaced regular expression pattern '[' by  '\\\\['")
            }
            else if (length(grep("[^\\\\]\\[<-", pattern))) {
                pattern <- sub("\\[<-", "\\\\\\[<-", pattern)
                warning("replaced '[<-' by '\\\\[<-' in regular expression pattern")
            }
        }
        grep(pattern, all.names, value = TRUE)
    }
    else all.names
}
<bytecode: 0x0000013623977928>
<environment: namespace:base>

rm(SOME_VARIABLE, ANOTHER_VARIABLE, etc) removes variable(s) from R’s memory

Use the rm() command to remove a variable from R’s memory

You can include the variable name in “quotes” or not include it in quotes. See the examples below.

# show all defined variables
ls()
 [1] "anne_johnsons_salary"              "change"                           
 [3] "jackSmithsSalary"                  "joesSalary"                       
 [5] "lettuce"                           "Lettuce"                          
 [7] "numberOfCandies"                   "numberOfChildren"                 
 [9] "oneOrange"                         "price1"                           
[11] "priceOfApple"                      "sue.cohens.salary"                
[13] "this.is.a.good.variable.name"      "this_is_also_a_good_variable_name"
# Remove the lettuce variable
rm(lettuce)

# show that lettuce is no longer defined - or just look at the Environment window
ls()
 [1] "anne_johnsons_salary"              "change"                           
 [3] "jackSmithsSalary"                  "joesSalary"                       
 [5] "Lettuce"                           "numberOfCandies"                  
 [7] "numberOfChildren"                  "oneOrange"                        
 [9] "price1"                            "priceOfApple"                     
[11] "sue.cohens.salary"                 "this.is.a.good.variable.name"     
[13] "this_is_also_a_good_variable_name"
# In quotes also works 
rm("change")

# show that change variable is no longer defined
ls()
 [1] "anne_johnsons_salary"              "jackSmithsSalary"                 
 [3] "joesSalary"                        "Lettuce"                          
 [5] "numberOfCandies"                   "numberOfChildren"                 
 [7] "oneOrange"                         "price1"                           
 [9] "priceOfApple"                      "sue.cohens.salary"                
[11] "this.is.a.good.variable.name"      "this_is_also_a_good_variable_name"

rm( list=ls() ) removes ALL variables

# To remove ALL of your variables, type the following command:
rm( list=ls() )

ls() displays “character(0)” when there are no variables defined.

When there are no variables at all, the ls() command displays “character(0)”. This may be confusing. We’ll explain why you get this confusing result a little later in the course.

# Above, we removed all the variables with the command "rm(list=ls())".
# When there are no variables, the "ls()" command shows "character(0)".
ls()    
character(0)

2.9 ———- PRACTICE ———-

#-----------------------------------------------------------.
# QUESTION:
#
# The price of an apple is $1.50. Create a variable named
# priceOfApple that contains that value.
#-----------------------------------------------------------.
priceOfApple = 1.50   # ANSWER
#-----------------------------------------------------------.
# QUESTION:
#
# Given the information below, write a command that creates a variable
# named "costOfPie" that stores the amount of money it takes to create a
# single apple pie. (do not use $ signs):
#
#   - The price of an apple is stored in the variable priceOfApple
#   - The price of a prepared pie crust is $7.50
#   - It takes 10 apples and one pie crust to make an apple pie.
#-----------------------------------------------------------.
costOfPie = priceOfApple * 10 + 7.50    # ANSWER
#-----------------------------------------------------------.
# QUESTION    ####
#
# ( see the information in the previous question )
# Write a command that stores the amount of money it takes to make 3 apple pies
# in the variable named totalAmount
#-----------------------------------------------------------.
totalAmount = costOfPie * 3   # ANSWER

2.10 Changing the value of a variable, eg. x = x + 5

# Let's get back to Joe's salary.
joesSalary = 50000

# if Sue's salary is 70000 you can store that information in another variable
suesSalary = 70000

# You can show the total salary for the company by adding together the two salaries
joesSalary + suesSalary
[1] 120000
# notice that joesSalary didn't change
joesSalary
[1] 50000
# suesSalary didn't change either
suesSalary
[1] 70000
# to figure out the total salary you can type the same command again
joesSalary + suesSalary
[1] 120000
# But typing the same commands over and over is not very efficient.
# Instead, you can create a new variable, e.g. totalSalary, that contains the result of
# adding together the two salaries.
totalSalary = joesSalary + suesSalary

# notice that when we type the above command that the "answer" isn't displayed. The command
# simply saves the result in the variable named, totalSalary. To see the result, you can
# type the variable name:
totalSalary
[1] 120000
# if want to give Joe a 10% raise. We can calculate the new Salary as follows:
joesSalary * 1.10
[1] 55000
# However, that still didn't actually change joesSalary
joesSalary
[1] 50000
# To actually change joesSalary, we can use an assignment statement as follows.
# The first step is that the value on the right hand side of the = sign is calculated using
# whatever values we already know. The 2nd step is that the variable on the left hand
# side of the = sign gets this new value.
joesSalary = joesSalary * 1.10

# Now let's see what joesSalary became
joesSalary
[1] 55000
# Notice that the totalSalary did NOT change.
totalSalary
[1] 120000
# To change the totalSalary, we would have to calculate it again.
totalSalary = joesSalary + suesSalary

# show the new totalSalary
totalSalary
[1] 125000

2.11 ———- PRACTICE ———-

# QUESTION:   
#
# There are 5 children. 
# Each package of candy costs $3.50 and contains 23 pieces.
# What is the maximum number of candies could we give to each child if we only have $15.00 to spend? 
# How much money will be left over? 
# How many candies will be left over?
#
# HINTS: 
# - create a variable, numPackages that contains the # of packages we can buy with $15.00
# - create a variable, totalCandies, that contains the total # of candies we will have
# - calculate the number of candies for each child
# - calculate the amount of money left over
# - calculate the number of candies left over

rm(list=ls())  # lets start with a clean slate     # ANSWER
numberOfChildren = 5                               # ANSWER
priceOfPackage = 3.50                               # ANSWER
candiesInPackage = 23                               # ANSWER
moneyToSpend = 15                               # ANSWER

numPackages = moneyToSpend %/% priceOfPackage        # ANSWER

totalCandies = numPackages * candiesInPackage        # ANSWER
candiesPerChild = totalCandies %/% numberOfChildren  # ANSWER
moneyLeftOver = moneyToSpend - priceOfPackage * numPackages # ANSWER

2.12 Other ways of assigning variables     =    <-     ->

#-------------------------------------------------------------.
# Other ways of assigning variables     =   <-  ->     ####
#
#    variable = value
#
#    variable <- value
#
#    value -> variable
#-------------------------------------------------------------.

# R allows an assignment statement to be written in 3 different ways. These all
# produce the exact same results.

# OPTION 1 - use an = sign
# This is what we've been doing all along up until now. Example:

numStudents = 32

# OPTION 2 - same as above but use "<-" instead of an = sign. The <- looks like
# an arrow. It consists of a less than sign followed by a minus sign. There may NOT
# be any spaces between the "<" and the "-"

numProfessors <- numStudents %/% 6

# OPTION 3 - same as above but the name of the varible goes on the right hand side
# and the "arrow" faces right ... e.g.

numProfessors - 1 -> numDeans

# show the variables values
numStudents
[1] 32
numProfessors
[1] 5
numDeans
[1] 4