Atomic Data Types
Exposition
Introduction
In this exercise we will explore three important atomic data types called numeric, logical, and character. We call them atomic because they can not be broken down into simpler data structures.1
Contrast this with complex data types that we will learn about in later lessons and which are composed by these atomic data types.
Numeric
You have already met the numeric data type in Rithmatic. How do you know? We can just ask R if 1
is numeric by typing and running is.numeric(1)
. Try it yourself:
is.numeric(1)
is.numeric(1)
is.numeric
is a name that refers to a method or recipe in R, called a function, that determines if the value it recieves is a number or not, i.e., is it numeric or not.
Now, try running is.numeric(2 + 2)
to see the result.
is.numeric(2 + 2)
is.numeric(2 + 2)
Logical
I hope you’ve noticed that R responds TRUE
when the value the is.numeric
function recieves is indeed numeric.
The value TRUE
is a kind of the next atomic data type we will explore, i.e., the logical data type.
TRUE
is R’s word for “yes.” R is case-sensitive, so TRUE
is not the same as true
, True
, or even TrUe
, so be sure to type it carefully. R can be very particular about such things.
TRUE
is not a numeric value. How can you find out?
is.numeric(TRUE)
is.numeric(TRUE)
Instead of TRUE
, R responds with FALSE
, which is R’s word for “no.” TRUE
and FALSE
are the only two possible values for the logical data type in R.
Can you guess how we test for logical values? Try running is.logical(FALSE)
.
is.logical(FALSE)
is.logical(FALSE)
If you ran is.logical(TRUE)
, what should the answer be? Type and check it below.
TRUE
TRUE
R will let you use T
for TRUE
and F
for FALSE
, but please don’t do that in this class or in your work. It makes your code harder to read for others, just as it made it harder for your 5th grade teacher to grade your true-false test. However, I want you to know that in case you see someone use it in their code or if your instructor is being lazy as he types in class. After all, that’s only what it should be—a shortcut. Note, though your instructor always types TRUE
and FALSE
out completely in his production code as 4–5 extra characters are worth the clarity.
OK, I’m pretty confident you have this now. What would is.logical(1)
return?
FALSE
FALSE
Character
The last important atomic data type you should know about for now is the character data type. The character type is used to represent text in R. So you know some of the lingo text for computers is often called character strings or simply strings.
R knows you intend something to be a string if you enclose it in quotes. The preferred style in R code is to use double quotes,
"Hello!"
but single quotes
'Hello!'
also work just fine and are sometimes important for getting your code to work right as we will see.
See what R does when you type and run "Hello, my name is R"
.
"Hello, my name is R"
"Hello, my name is R"
Even though double quotes are preferred, single quotes can be used. Let’s test that by typing and running 'What is your name?'
.
'What is your name?'
'What is your name?'
Since the is.logical
function tests whether something is of the logical datatype, and is.numeric
tests whether something is of the numeric data type, what function do you think tests whether something is of the character type?
Go for it! Test whether
"Hi!"
is of type character.
is.character("Hi!")
is.character("Hi!")
Experimentation
Now it is time to try out your skills. Experiment below and figure out which atomic data type (numeric, logical, or character) is the result of dividing another number by zero.
What did you conclude? Type just numeric
, logical
, or character
below.
numeric
numeric
You know what is.character(2)
will return, right? … FALSE
But what if you wanted to use is.character
and the number 2
together, and get TRUE
instead (i.e., what would you need to add surrounding the number 2
to get TRUE
)?
Type the full expression using is.character
and the number 2
that returns TRUE
.
is.character("2")
is.character("2")
in to make R recognize it as a
What do you enclose a group of characters character type?
Exposition, part 2
Logical Operators
Useful operators for the logical data type include &
, |
, and !
. They are called logical operators. The |
is often called a pipe character.
|
,
above the backslash, \
, on a key at the right edge
of your keyboard, i.e.,
|
character.
Let’s try each of the logical operators out, starting with the !
operator. In the code cell below, try evaluating both !TRUE
and !FALSE
below, then answer the questions that follow.
What does the !
operator return when applied to TRUE
?
FALSE
FALSE
What does the !
operator return when applied to FALSE
?
TRUE
TRUE
Now, experiment with the &
and |
operators. Try all combinations of TRUE
and FALSE
(e.g., TRUE & FALSE
, FALSE | FALSE
, etc.), then answer the questions that follow.
What is TRUE & FALSE
?
FALSE
FALSE
What is TRUE | FALSE
?
TRUE
TRUE
What is TRUE | TRUE
?
TRUE
TRUE
Which operator name best fits &
? Type either "or"
, "and"
, or "not"
. Don’t forget the double quotes!
"and"
"and"
Which operator name best fits |
? Type either "or"
, "and"
, or "not"
.
"or"
"or"
The !
operator is different from the others you have seen so far in that it only takes one input (argument), whereas the others take two. One-argument operators are called unary, while those that take two are called binary. Those that take more than two are called multiary or multary. So !
is unary while +
, &
, and others you have learned so far are binary. We have not yet met any multiary operators: 2 + 2 + 3
is just using a binary operator twice.
Relational Operators
Another group of useful binary operators for all the atomic data types are the six relational operators: <
, >
, <=
, >=
, ==
, and !=
. Most situations make sense intuitively, especially with numerics, when you know their meaning.
The symbol <
means “less than”, and >
means “greater than”. So, 5 > 6
is FALSE
and 5 < 6
is TRUE
. The symbol <=
means “less than or equal to”. So, 5 <= 5
is TRUE
as is 5 <= 6
.
Two equal signs without any space between them ==
is the test of whether two things are equal or not. A single equals sign =
means something different, so be careful or you will get an error message from your code or worse an unsuspected logical error (a “bug”) that may surprise you! The symbol !=
tests whether two things are not equal.
So, 3 == 3
is TRUE
and 2 != 3
is TRUE
.
Let’s test your knowledge. What is 5 == 10
?
FALSE
FALSE
What is 8 > 10
?
FALSE
FALSE
What is 100 < 101
?
TRUE
TRUE
What is "a" < "b"
? (Trust your gut on this one!)
TRUE
TRUE
So, that probably made sense but does the fact that "B"
is less than "a"
make sense? Perhaps not, but the capitals are all less than the lower case letters and most (if not all) punctuation is less than upper case letters and thus the lower case letters too.
What is "b" > "!"
?
TRUE
TRUE
What is "A" == "a"
?
FALSE
FALSE
What is TRUE > FALSE
?
TRUE
is the answer! After all, truth is indeed greater than falsity! Now you have a good sense of how the relational operators work on all the data types we’ve learned about.
Evaluation
Submit Your Assignment
Footnotes
Much like we now know that atoms can, in fact, be split into smaller subatomic particles, these atomic data types can also be split further, but that is far beyond the scope of this course.↩︎