Name: Achmad Fahry Baihaki
NIM: 2206065110100
Institute: Maulana Malik Ibrahim Islamic State University of Malang
Departement: Computer Science
Lecturer: Prof. Dr. Suhartono, M.Kom


Data Types

A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, realtional or logical operations can be applied to it wihout causing an error.

In R language, data is classified into several types. Data types in R include:

Data Type Example Description
Logical TRUE, False Boolean Value
Numeric 9.5, 9, 999 Various types of numbers
Integer 23L, 97L, 3L Integer numbers
Complex 2i, 3i, 9i Complex numbers
Character ‘x’, “y”, “999” Characters and Strings
Factor 1, 0, “Red” Can be numeric or string (read as a number in the process)
Raw Synonymous with the word “hello” Any type of data stored as raw bytes

Syntax above is some examples of data types in R. To check the data types in an object, we can use the class() command.

Here’s the example:

# Logical

kiwi <- TRUE

class(kiwi)
## [1] "logical"
# Numeric

x <- 9.5

class(x)
## [1] "numeric"
# Complex

a <- 5 + 9i

class(a)
## [1] "complex"
# Character/String

b <- "Hola, amigos!"

class(b)
## [1] "character"
# Raw (we need to convert first)

ax <- charToRaw("Hola, amigos!")

class(ax)
## [1] "raw"

The sixth of data types above is known as the atomic data type. That’s because it can only handle one data type. For example only numeric or only integer.

Instead of using the class() command, we also can use is.numeric(), is.character(), is.logical and others based on data types that we want to check. Different with the class() command, if we use is. command function the output will be boolean data type. So this command is only used to check whether the data type of the object/variable matches what we think.

Here’s the example of is. command:

y <- 95

# Check whether the object/variable is a numeric or not
is.numeric(y)
## [1] TRUE
# Check whether the y object/variable is a character or not
is.character(y)
## [1] FALSE

We also can change the data types to other types, like integer to numeric vice versa. To do that, we can use as.numeric() command. Or if we want to change to the other types, we can use the command below.

Here’s the example:

# Integer
kiwi <- 2L

# Change to numeric
as.numeric(kiwi)
## [1] 2
# Check
is.numeric(kiwi)
## [1] TRUE
# Character
orange <- "Fruit"

# Change to factor
as.factor(orange)
## [1] Fruit
## Levels: Fruit
# Character
val <- "Drugs"

# Change to numeric
as.numeric(val)
## Warning: NAs introduced by coercion
## [1] NA

We can’t change the character data type that the value is not a number, but if the value is a number, it can change to numeric.

Data Structures

A data structure is a storage that is used to store, organize and retrieve data. It is a way of arranging data on a computer so that it can be accessed and updated efficiently.

For example, in a database, each field is separate and its information can be retrieved separately or together with data from other fields in various combinations.

Here’s the data classifications:

Dimension Homogeneous Heterogeneous
1d Atomic Vector List
2d Matrix Dataframe
nd Array

Objects with homogeneous data structures can only store one data type or data type (only numeric or only factors), while objects with heterogeneous data structures can store different data types.