2. Meet the Data Cast

Before you journey deeper into the R universe, you meet the main data cast. You will learn the various data types commonly encountered in programming.
Author

Nien Xiang Tou

Published

September 24, 2024

Image generated by generative AI.

Learning Goals

  • Learn the various data types
  • Learn how to check the data type
  • Learn how to change the data type

Data Types

Data types are key in programming language. Different data types support different operations. For example, arithmetic operations can only be performed on numeric but not character data. Here are some common data types.

Data Type Description Examples
Numeric Real numbers that are either integers or contain decimal points. 3.14
Integer Specific numeric data that represent whole numbers without decimal points. 37
Character Text or string values enclosed in either single or double quotes. "Hello World"
Logical Data that take on the value of either TRUE or FALSE. 5 > 0

Numeric Data - Take note!

By default, any number you enter in R is considered numeric. You may add the L suffix after a whole number to denote it as an integer instead.

Checking data types

One common way of checking the data type of an object is using the class() function.

class("Hello World")
_webr_editor_1 = Object {code: null, options: Object, indicator: Ke}

Exercise 2.1

The code below creates objects of various data types.

a <- 73
b <- 73/7
c <- b > a
_webr_editor_2 = Object {code: "a <- 73\nb <- 73/7\nc <- b > a", options: Object, indicator: Ke}

Use the class() function to check the data type of the objects above and assign your answers to the respective objects.

class_a <- ______
class_a

class_b <- ______
class_b

class_c <- ______
class_c
_webr_editor_3 = Object {code: null, options: Object, indicator: Ke}
class_a <- class(a)
class_b <- class(b)
class_c <- class(c)

Checking specific data types

There are several alternative ways of checking data types. One useful way of verifying specific data types is employing the is.*() functions. These functions return a logical value.

# x is an object assigned the value of pi to ten decimal places
x <- 3.1415926535

# To check if x is a numeric
is.numeric(x)

# To check if x is an integer
is.integer(x)

# To check if x is a character
is.character(x)
_webr_editor_4 = Object {code: null, options: Object, indicator: Ke}

Converting data types

As mentioned above, certain functions only support specific data types. Thus, it is important to ensure the compatibility between your data type and the functions to execute. You can change the data type using the as.*() functions.

obj <- 37
class(obj)
_webr_editor_5 = Object {code: "obj <- 37\nclass(obj)", options: Object, indicator: Ke}

Run the code below to convert the object to the respective data types.

# Convert object to an integer type
int_obj <- as.integer(obj)
int_obj
class(int_obj)

# Convert object to a character type
cha_obj <- as.character(obj)
cha_obj
class(cha_obj)
_webr_editor_6 = Object {code: null, options: Object, indicator: Ke}

Both object outputs look similar but differ in data types. The code below performs a multiplication operation on the objects. Run the code to examine the difference between them.

int_obj*2
_webr_editor_7 = Object {code: null, options: Object, indicator: Ke}
cha_obj*2
_webr_editor_8 = Object {code: null, options: Object, indicator: Ke}

Exercise 2.2

You are given two objects named obj_1 and obj_2 respectively. Run the code below to see their output.

obj_1 <- "37"
obj_2 <- 73
print(obj_1)
print(obj_2)
_webr_editor_10 = Object {code: null, options: Object, indicator: Ke}

Check the data types of the two objects.

_webr_editor_11 = Object {code: null, options: Object, indicator: Ke}
Hint 1

Use either class() or is.*() functions. Refer to the sections above for examples on these functions.

class(obj_1)
class(obj_2)

# Alternatively, you may check whether they are numeric objects
is.numeric(obj_1)
is.numeric(obj_2)

Ensure that both objects are numeric data types and assign them to the respective objects. Convert the data type of the objects if needed, and perform a sum operation on both objects.

num_obj_1 <- ______

num_obj_2 <- ______

sum(num_obj_1, num_obj_2)
_webr_editor_12 = Object {code: null, options: Object, indicator: Ke}
Hint 1

Use as.*() functions. Refer to the sections above for examples on these functions.

num_obj_1 <- as.numeric(obj_1)
num_obj_2 <- as.numeric(obj_2)
sum(num_obj_1, num_obj_2)

Bonus: Factor Data

Factor data is a specific data type in R that represents categorical data. It contains a fixed set of levels, stored as integer codes with corresponding labels.

# Example vector of shirt sizes
sizes_char <- c("Small", "Medium", "Large", "Medium", "Small", "Large",
"Extra Large", "Extra Small")

# Display the vector
print(sizes_char)
_webr_editor_13 = Object {code: null, options: Object, indicator: Ke}
# Convert to factor data type
sizes_factor <- as.factor(sizes_char)

# Display the vector
print(sizes_factor)
_webr_editor_14 = Object {code: null, options: Object, indicator: Ke}

By default, factor levels follow either numerical values or alphabetical orders. You may specify your own levels of the factor.

# Specifying the size levels
size_levels <- c("Extra Small","Small", "Medium", "Large","Extra Large")

# Convert to factor data type with specified levels
sizes_factor_v2 <- factor(sizes_char,
levels = size_levels)

# Display the vector
print(sizes_factor_v2)
_webr_editor_15 = Object {code: null, options: Object, indicator: Ke}
Downloading webR