Exercise

In this exercise we will explore three important atomic data types: numeric, character, and logical.

Numeric

You have already met numeric in the Rithmatic exercise. How do you know? Just ask R:

is.numeric(1)
## [1] TRUE
is.numeric(2 + 2)
## [1] TRUE

Based on the name I’m sure you are not surprised that the numeric data type is used for numbers.

Character

The character type is used to represent text, or what is more commonly called strings in computer lingo. You know you have string if it is enclosed in quotes.
Those quotes can be either double (") or single ('). In this class we will favor the double quotes for consistency but there are instances (see below) where it is easiest to use the other type.

"Hello, my name is R"
## [1] "Hello, my name is R"
"What is your name?"
## [1] "What is your name?"
is.character("Hi, _____")  # NOTE: _ is an underscore or underline character
                           # it is usually above your hyphen (i.e., -)
## [1] TRUE

Logical

The logical type is used to simply represent TRUE or FALSE. This is sort of R’s way of saying “yes” or “no.”

TRUE
## [1] TRUE
FALSE
## [1] FALSE
is.logical(TRUE)
## [1] TRUE
is.logical(2)
## [1] FALSE

R allows you to use T for TRUE and F for FALSE, but please do not use that in this class. It makes your code harder to read for others. However, I want you to know in case you see someone else do it or if your instructor is being lazy one day at the console. (However, your instructor always types it out completely in his production code.)

T
## [1] TRUE
is.logical(F)
## [1] TRUE

Explore and Extend

!TRUE
## [1] FALSE
TRUE & FALSE
## [1] FALSE

The ! operator is different from the others you have seen so far in that it only takes one input, or argument, whereas the others take two. One argument operators are called unitary while those that take two are called binary. Those that take more than two are called multiary or multary. So ! is unitary while +, &, and all the others you have learned are binary. We have not yet met any multiary operators: 2 + 2 + 3 is just using a binary operator twice.

Data type: numeric character logical
Evaluates to: TRUE FALSE TRUE FALSE TRUE FALSE
< 2 < 3 3 > 2 "a" < "b" "b" < "a" FALSE < TRUE TRUE < FALSE
>
<= 3 <= 3 3 <= 2
>=
==
!=

Evaluate

Complete the following exercises in R and turn in your code in an .R file. Like before, name your code file atomic_<yourlastname>.R and if some part of the response is not an executable by R without an error use a comment.

  1. How many arguments does a binary operator take? Give an example.

  2. Fix the following code so it does not generate an error. Why does R generate an error?

True
  1. Without running it in R, what should R say when you run the following code? Once you feel confident, check it in R. Turn in the answer.
(2 + 2 == 4) | (3 + 3 == 4)
  1. Fix the following code so it does not generate an error (notice the code highlighting/coloration trying to help you out?):
is.character('I'm fine')
  1. (optional: challenging) Remember precedence from the Rithmatic exercise? Do you need the parentheses above? Write out the precedence for the arithmetic operators (*, /, +, -), logical operators (!, &, |), and relational operators (<, >, <=, >=, ==, !=) by writing each group with equal precedence on a single line and writing operators with higher precedence on a higher line. (Hint: your “table” should have 6 lines and all the relational operators will be on the same line.) For example, as you know for the arthimetic operators, the “table” would look like this, with two lines:
# * /
# + -