In this exercise we will explore three important atomic data types: numeric
, character
, and logical
.
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.
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
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
Which of the atomic data types is the answer you get when you divide another number by zero? Does that surprise you?
What does is.character(2)
evaluate to? How can you still use is.character
and 2
together, but get TRUE
instead?
What happens when you type "John said, "How are you today?""
in the R console? Think about this. Why do you think this happens? Propose a solution.
Useful operators for the logical
data type include &
, |
, and !
. They are called the logical operators. The |
is often called a pipe character. You’ll usually find it above the backslash (i.e., \
) on your keyboard. Make a table for all possible values of TRUE
and FALSE
. (Hint: only two for !
, but four possibilities for &
and |
.) What would you propose as names for these?
!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.
<
, >
, <=
, >=
, ==
, and !=
. Write an expression using each atomic data type and each relational operator in the following table that I started for you: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
|
||||
>=
|
||||||
==
|
||||||
!=
|
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.
How many arguments does a binary operator take? Give an example.
Fix the following code so it does not generate an error. Why does R generate an error?
True
TRUE
(2 + 2 == 4) | (3 + 3 == 4)
## [1] TRUE
is.character('I'm fine')
is.character("I'm fine")
## [1] TRUE
*
, /
, +
, -
), 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:# * /
# + -
# * /
# + -
# < > <= >= == !=
# !
# &
# |
#To solve this you need expressions that evaluate differently
#depending on the order you execute them in (use parentheses to group)
#then see what R does without the parentheses:
# * has higher precedence than <
# and as the problem says all the relational operators
# are equal in precedence
(2 * 3) < (3 * 2)
2 * (3 < 3) * 2
2 * 3 < 3 * 2
# ! has lower precedence than <
(!TRUE) < FALSE
!(TRUE < FALSE)
!TRUE < FALSE
# ! has higher precedence than &
!TRUE & FALSE
(!TRUE) & FALSE
!(TRUE & FALSE)
# & has higher precedence than !
FALSE & FALSE | TRUE
(FALSE & FALSE) | TRUE
FALSE & (FALSE | TRUE)