Objects can contain data in a variety different of formats. In this course, we will focus onĀ Values, Vectors, Matrices, Data frames, and Lists. Each is explained below.

Values

Values represent single entries of data. They can be of any class.

x <- 2
x <- 'froggy'

Vectors

Vectors represent 1-dimensional data (a single row) with several values. We often create vectors using the function c().

x <- c(2,3,4,5,6)
x
y <- c('red','blue','green','yellow')
y
## [1] 2 3 4 5 6
## [1] "red"    "blue"   "green"  "yellow"

Values can be of any class, however, be aware that mixing classes will result in the character class for all the values.

z <- c(2,3,4,'Apple','Orange','Frogpear')
z
class(z)
## [1] "2"        "3"        "4"        "Apple"    "Orange"   "Frogpear"
## [1] "character"

Matrix

A Matrix represents 2 dimensional data (columns and rows) where the classes of data (numeric, logical, character, etc.) are all the same

mat <- matrix(c(c(1,2,3,4,5),
                 c(2,3,4,5,6),
                 c(3,4,5,6,7),
                 c(1,4,5,6,5)), ncol = 5)
mat
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    5    5    5    4
## [2,]    2    2    6    6    5
## [3,]    3    3    3    7    6
## [4,]    4    4    4    1    5

Similar to a vector, mixing the data classes will result in the data becoming all of the class character.

mat <- matrix(c(c(TRUE,2,3,4,5),
                 c(2,3,FALSE,5,6),
                 c("a","frog","fog","frg","frog_2.0"),
                 c(1,4,5,6,5),
                c(4,5,8,2,1)), ncol = 5)
mat
##      [,1] [,2] [,3]       [,4] [,5]
## [1,] "1"  "2"  "a"        "1"  "4" 
## [2,] "2"  "3"  "frog"     "4"  "5" 
## [3,] "3"  "0"  "fog"      "5"  "8" 
## [4,] "4"  "5"  "frg"      "6"  "2" 
## [5,] "5"  "6"  "frog_2.0" "5"  "1"

Data frames

Data frames are similar to matrices in that they are 2-dimensional sets of data. However, they contain columns where each class can be different.

This is the data type you will most often import your datasets as into R. In the example below, I am creating a data frame by combining equal-length vectors via the data.frame() function. Notice that each vector is of a different class.

age <- c(1,2,5,4,5,4,2,3,7,6)
size_cm <- c(1.5,2.6,1,1,5,6,2.7,4,9.8,10)
habitat <- c("Urban","Urban","Urban","Suburban","Suburban","Suburban","Rural","Rural","Rural","Rural")
true_false <- c(T,T,F,T,F,T,T,T,F,F) #Using shorthand True/False
df <- data.frame(age,size_cm,habitat, true_false)
df
##    age size_cm  habitat true_false
## 1    1     1.5    Urban       TRUE
## 2    2     2.6    Urban       TRUE
## 3    5     1.0    Urban      FALSE
## 4    4     1.0 Suburban       TRUE
## 5    5     5.0 Suburban      FALSE
## 6    4     6.0 Suburban       TRUE
## 7    2     2.7    Rural       TRUE
## 8    3     4.0    Rural       TRUE
## 9    7     9.8    Rural      FALSE
## 10   6    10.0    Rural      FALSE

Lists

Lists contain multiple objects within them. These can be any type of object such as single values, data frames, or even other lists! We will not use lists too often in this course, but they are very common outputs from functions in R.

The below example shows a list containing several vectors of equal length.

list <- list(x = c(1,2,3,4,5,6), 
     y = c(1,45,67,54,23),
     z = c(5,5))
list
## $x
## [1] 1 2 3 4 5 6
## 
## $y
## [1]  1 45 67 54 23
## 
## $z
## [1] 5 5

All these different object and data types are critical to understanding R. Throughout the course we will refer to different types of data by their class and structure.

Last modified: Tuesday, 2 December 2025, 4:46 AM