Lesson 2 Introduction to data types
We will start with some of the most basic data units in R and work our way up to some of the most complex. The most basic unit we will deal with are data types. There are many data types in R, but we will mostly deal with four: characters, numerics, integers, and logicals.
2.1 Character
A character data type is used to store text.
## [1] "Hello, World"
If you run this code, you will have just created a character data type.
We can use the class
function to get the data type or data structure. More
about functions later! For now, you should know that you can ask R about the
data type by enclosing it in parentheses after the word class
.
## [1] "character"
You will notice R tells you the class is “character”.
2.2 Numeric
A numeric data type is used to store real numbers.
## [1] 4.78
## [1] "numeric"
An integer is a special subset of the numeric data type used to store integers. When we want to tell R we want a number as an integer, we type an “L” after the number.
## [1] 4
## [1] "integer"
2.3 Logical
A logical type takes two possible values: “TRUE” or “FALSE”. Note these values are always capitalized. R also recognizes shortforms for logicals “T” and “F”.
## [1] TRUE
## [1] "logical"
## [1] "logical"
2.4 Checking the data type
We can also ask R whether a data type is of a specific type using the “is” set of functions. We use this function by typing is.DATATYPE() and enclosing the data type we aren’t sure about in the parentheses. For example is.numeric(3L)
asks R whether 3L is numeric and returns a logical datatype specifying whether the statement is TRUE (yes, it’s a numeric) or FALSE (no, it’s not a numeric). Here are some examples. Before running the code, try to predict what classes R will return. You should also try some others yourself!
## [1] FALSE
## [1] TRUE
## [1] TRUE