The Basics of R and RStudio

Part 3: Data Types

RStudio
R
Tutorial
Blog
Author

William Okech

Published

June 23, 2022

Introduction

R and RStudio utilize multiple data types to store different kinds of data.

The most common data types in R are listed below.

Data Type Description
Numeric The most common data type. The values can be numbers or decimals (all real numbers).
Integer Special case of numeric data without decimals.
Logical Boolean data type with only 2 values (TRUE or FALSE).
Complex Specifies imaginary values in R.
Character Assigns a character or string to a variable. The character variables are enclosed in single quotes (‘character’) while the string variables are enclosed in double quotes (“string”).
Factor Special type of character variable that represents a categorical such as gender.
Raw Specifies values as raw bytes. It uses built-in functions to convert between raw and character (charToRaw() or rawToChar()).
Dates Specifies the date variable. Date stores a date and POSIXct stores a date and time. The output is indicated as the number of days (Date) or number of seconds (POSIXct) since 01/01/1970.

Data types

1. Numeric

89.98
[1] 89.98
55
[1] 55

2. Integer

5L
[1] 5
5768L
[1] 5768

3. Logical

TRUE
[1] TRUE
FALSE
[1] FALSE

4. Complex

10 + 30i
[1] 10+30i
287 + 34i
[1] 287+34i

5. Character or String

'abc'
[1] "abc"
"def"
[1] "def"
"I like learning R"
[1] "I like learning R"

6. Dates

"2022-06-23 14:39:21 EAT"
[1] "2022-06-23 14:39:21 EAT"
"2022-06-23"
[1] "2022-06-23"

Examining various data types

Several functions exist to examine the features of the various data types. These include:

  1. typeof() – what is the data type of the object (low-level)?
  2. class() – what is the data type of the object (high-level)?
  3. length() – how long is the object?
  4. attributes() – any metadata available?

Let’s look at how these functions work with a few examples

a <- 45.84
b <- 858L
c <- TRUE
d <- 89 + 34i
e <- 'abc'

1. Examine the data type at a low-level with typeof()

typeof(a)
[1] "double"
typeof(b)
[1] "integer"
typeof(c)
[1] "logical"
typeof(d)
[1] "complex"
typeof(e)
[1] "character"

2. Examine the data type at a high-level with class()

class(a)
[1] "numeric"
class(b)
[1] "integer"
class(c)
[1] "logical"
class(d)
[1] "complex"
class(e)
[1] "character"

3. Use the is.____() functions to determine the data type

To test whether the variable is of a specific type, we can use the is.____() functions.

First, we test the variable a which is numeric.

is.numeric(a)
[1] TRUE
is.integer(a)
[1] FALSE
is.logical(a)
[1] FALSE
is.character(a)
[1] FALSE

Second, we test the variable c which is logical.

is.numeric(c)
[1] FALSE
is.integer(c)
[1] FALSE
is.logical(c)
[1] TRUE
is.character(c)
[1] FALSE

Converting between various data types

To convert between data types we can use the as.____() functions. These include: as.Date(), as.numeric(), and as.factor(). Additionally, other helpful functions include factor() which adds levels to the data and nchar() which provides the length of the data.

Examples

as.integer(a)
[1] 45
as.logical(0)
[1] FALSE
as.logical(1)
[1] TRUE
nchar(e)
[1] 3