April 13, 2018
2 + 2 becomes 10100010 11101011Input: Slices of bread, slices of tomato, pieces of lettuce, strips of cooked bacon
Output: BLT sandwich
put down a slice of bread
put a slice of tomato on top of the bread
put a piece of lettuce on top of the tomato
put 3 strips of cooked bacon on top of the lettuce
put a second slice of bread on top of the bacon
return a BLT sandwich
Input: Two variables: var1 and var2
Output: Same variables with values swapped
Copy value of var1 to temp
Copy value of var2 to var1
Copy value of temp to var2
2 + 2 is syntactically correct, but + 2 2 isn’t2 + 2 means add 2 to 2CSULB_Intro_R.Rproj – please open it in RStudio.<- and =+, subtraction -, multiplication *, division /, etc.>, less than <, equal to ==, etc.X != xLet’s try some simple assignment operations…
n <- 10 # assign the value 10 to an object named n
n # look at what R stored it as## [1] 10
N <- 5 # assign the value 5 to an object named N
N # inspect the result## [1] 5
n == N # are n and N equal?## [1] FALSE
n + N # add n and N## [1] 15
Let’s implement our BLT sandwich in R
# ingredients
bread <- "slice of bread"
tomato <- "slice of tomato"
lettuce <- "piece of romaine lettuce"
bacon <- "cooked strips of bacon"
# output the sandwich
print(bread)
print(bacon)
print(lettuce)
print(tomato)
print(bread)## [1] "slice of bread"
## [1] "cooked strips of bacon"
## [1] "piece of romaine lettuce"
## [1] "slice of tomato"
## [1] "slice of bread"
Let’s implement our swap algorithm in R
# inputs
var1 <- 10
var2 <- 15
# perform the swap
temp <- var1
var1 <- var2
var2 <- temp
# inspect the results
var1## [1] 15
var2## [1] 10