<- c(1, 3, 3, 7)
x
print(x)
[1] 1 3 3 7
In computer science, a data structure refers to the method which one uses to organize their data. Six basic data structures are commonly used in R:
We can create a vector by using the “c” function to combine multiple values into a single vector. In the following example, we will combine four separate numbers into a single vector and the output the resulting vector to see what it looks like.
<- c(1, 3, 3, 7)
x
print(x)
[1] 1 3 3 7
Lists are a collection of objects. This means that each element can be a different data type (unlike vectors). In the following example we’ll create a list containing two character objects and one vector with the “list” function.
<- "John"
first_name <- "Smith"
last_name <- c(1, 3, 3, 7)
favorite_numbers
<- list(first_name, last_name, favorite_numbers)
person
print(person)
[[1]]
[1] "John"
[[2]]
[1] "Smith"
[[3]]
[1] 1 3 3 7
A matrix is a two-dimensional array where the data is all of the same type. In the following example, we’ll create a matrix with three rows and four columns.
<- matrix(
x c(1,3,3,7,1,3,3,7,1,3,3,7)
nrow = 3
, ncol = 4
, byrow = TRUE)
,
print(x)
[,1] [,2] [,3] [,4]
[1,] 1 3 3 7
[2,] 1 3 3 7
[3,] 1 3 3 7
Factors are used to designate levels within categorical data. In the following example, we’ll use the “factor” function on a vector of assorted color names to receive the “levels” which it contains.
<- c("Red", "Blue", "Red", "Yellow", "Yellow")
x
<- factor(x)
colors
print(colors)
[1] Red Blue Red Yellow Yellow
Levels: Blue Red Yellow
A data frame contains two-dimensional data. Unlike the matrix data structure, each column of a data frame can contain data of a differing type (but within a column the data must be of the same type). The following example will create a data frame with two rows and two columns.
<- c("John", "Jane")
people <- c(1, 2)
id <- data.frame(id = id, person = people)
df
print(df)
id person
1 1 John
2 2 Jane
Arrays are objects that can have more than two dimensions. This is sometimes referred to as being “n-dimensional”. The dimensions of the following example are 1 x 4 x 3. You’ll see that the data consist of one row and four columns spread out over a third dimension.
<- array(
x c(1,3,3,7,1,3,3,7,1,3,3,7)
dim = c(1,4,3))
,
print(x)
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 3 3 7
, , 2
[,1] [,2] [,3] [,4]
[1,] 1 3 3 7
, , 3
[,1] [,2] [,3] [,4]
[1,] 1 3 3 7