Codecademy Logo

Data Structures

R Data Frames

A data frame is a spreadsheet-like R object that stores data in two dimensions represented by columns and rows. The columns are different variables of the data frame and the rows are the observations of each variable.

We can navigate data frames using either the $ operator and specifying a column name, or by using [r,c] where r represents the row index and c represents the column index.

# Initiate data frame
dat <- data.frame(
x = c(1,1,1,1),
y = c(2,2,2,2),
z = c(3,3,3,3))
# Call column y
# Call row 3
# Call row 1 column 3

R vectors

A vector is a data structure that can hold multiple objects of the same data type. For example, c(45, 25, 89, 10) creates a numerical vector. To access elements, use the [ ] brackets with indexing starting from 1.

my_string_vector <- c("this", "is", "an", "example", "vector")
my_string_vector[3] # returns "an"
my_boolean_vector <- c(TRUE, FALSE, FALSE)
my_numerical_vector <- c(0.4, 0.9, 1, 0.45, 1.2, 0.33)
# R will force the same type even if you input different types to c()
my_vector <- c("word", 45, 12, FALSE)
# result: a vector of strings "word" "45" "12" "FALSE"

R Matrices

In R, a matrix is a two-dimensional data structure that holds objects of the same type. To create one, use the matrix() function with a vector representing the data (R will interpret whether it is repeating or non-repeating), nrow equal to the number of rows, and ncol equal to the number of columns.

To access a single element inside a matrix called ex_matrix, you can look up the item in the rth row and the cth column like so: ex_matrix[r, ].

To access the entire rth row, you can refer to it like so: ex_matrix[r, c]. To access the cth column, use ex_matrix[ , c].

# will create a 3-by-3 matrix with the vectors being filled column-wise (default)
my_matrix <- matrix(c("a", "b", "e", "k", "e", "w", "g", "x", "t"), nrow = 3, ncol = 3)
# result:
# "a" "k" "g"
# "b" "e" "x"
# "e" "w" "t"
single_element <- my_matrix[1, 3]
# returns "g"
second_row <- my_matrix[2,]
# returns: "b" "e" "x"
second_col <- my_matrix[,2]
# returns: "k" "e" "w"

R Lists

In R, a list can store a variety of data types and data structures in a single variable. This means a list can hold individual strings and numbers, but also vectors and lists.

To access a single element, use [[ ]] double brackets. A list is useful for organization because you can name the elements inside it and select them using $

my_list <- list("Elephant", FALSE, 900, 80.3, list("pencil", "pens"))
my_list[[4]] # returns 80.3
my_longer_list <- list(misc = my_list, notes = c("g", "b", "d", "g"))
# returns the contents of my_list
# returns the contents of my_list
# returns "g" "b" "d" "g"

Learn More on Codecademy