Chapter 4 Data Types in R
4.1 Introduction
In this chapter, we will learn about the following data types:
- numeric/double
- integer
- character
- logical
- date/time
4.2 Numeric
In R, numbers are represented by the data type numeric
. We will first create a variable and assign it a value. Next we will learn a few methods of checking the type of the variable.
# create two variables
<- 3.5
number1 <- 3
number2
# check data type
class(number1)
## [1] "numeric"
class(number2)
## [1] "numeric"
# check if data type is numeric
is.numeric(number1)
## [1] TRUE
is.numeric(number2)
## [1] TRUE
If you carefully observe, integers
are also treated as numeric/double
. We will learn to create integers in a while. In the meanwhile, we have introduced two new funtions in the above example:
class()
: returns theclass
ortype
is.numeric()
: tests whether the variable is of typenumeric
4.3 Integer
Unless specified otherwise, integers are treated as numeric
or double
. In this section, we will learn to create variables of the type integer
and to convert other data types to integer
.
- create a variable
number1
and assign it the value3
- check the data type of
number1
usingclass
- create a second variable
number2
usingas.integer
and assign it the value3
- check the data type of
number2
usingclass
- finally use
is.integer
to check the data type of bothnumber1
andnumber2
# create a variable and assign it an integer value
<- 3
number1
# create another variable using as.integer
<- as.integer(3)
number2
# check the data type
class(number1)
## [1] "numeric"
class(number2)
## [1] "integer"
# use is.integer to check data type
is.integer(number1)
## [1] FALSE
is.integer(number2)
## [1] TRUE
4.4 Character
Letters, words and group of words are represented by the data type character
. All data of type character
must be enclosed in single or double quotation marks. In fact any value enclosed in quotes will be treated as character
. Let us create two variables to store the first and last name of a some random guy.
# first name
<- "jovial"
first_name
# last name
<- 'mann'
last_name
# check data type
class(first_name)
## [1] "character"
class(last_name)
## [1] "character"
# use is.charactert to check data type
is.character(first_name)
## [1] TRUE
is.character(last_name)
## [1] TRUE
You can coerce any data type to character
using as.character()
.
# create variable of different data types
<- as.integer(30) # integer
age <- 9.8 # numeric/double
score <- TRUE # logical
opt_course <- Sys.time() # date time
today
as.character(age)
## [1] "30"
as.character(score)
## [1] "9.8"
as.character(opt_course)
## [1] "TRUE"
as.character(today)
## [1] "2021-06-02 16:25:27"
4.5 Logical
Logical data types take only 2 values. Either TRUE
or FALSE
. Sich data types are created when we compare two objects in R using
comparison or logical operators.
- create two variables
x
andy
- assign them the values
TRUE
andFALSE
respectively - use
is.logical
to check data type - use
as.logical
to coerce other data types tological
# create variables x and y
<- TRUE
x <- FALSE
y
# check data type
class(x)
## [1] "logical"
is.logical(y)
## [1] TRUE
The outcome of comparison operators is always logical
. In the below example, we compare two numbers to see the outcome.
# create two numeric variables
<- 3
x <- 4
y
# compare x and y
> y x
## [1] FALSE
< y x
## [1] TRUE
# store the result
<- x > y
z class(z)
## [1] "logical"
TRUE
is represented by all numbers except 0
. FALSE
is represented only by 0
and no other numbers.
# TRUE and FALSE are represented by 1 and 0
as.logical(1)
## [1] TRUE
as.logical(0)
## [1] FALSE
# using numbers
as.numeric(TRUE)
## [1] 1
as.numeric(FALSE)
## [1] 0
# using different numbers
as.logical(-2, -1.5, -1, 0, 1, 2)
## [1] TRUE
Use as.logical()
to coerce other data types to logical
.
# create variable of different data types
<- as.integer(30) # integer
age <- 9.8 # numeric/double
score <- TRUE # logical
opt_course <- Sys.time() # date time
today
as.logical(age)
## [1] TRUE
as.logical(score)
## [1] TRUE
as.logical(opt_course)
## [1] TRUE
as.logical(today)
## [1] TRUE