Skip to main content

Section 1.6 Data Frames

Vectors are an essential way to store information, but specifically, one type of information. When we tried to mix different types of data in one vector, we saw that R did not like that, and considered everything as part of a character string. So, what if we want to be able to have different types of data all in one place?
This is exactly where data frames come into play. Data frames are the most common way to view data, with maybe the most prominent examples being Microsoft Excel.

Subsection 1.6.1 Creating Your Own Data Frame

Imagine you have three vectors and you want to turn them into a data frame, with each of the three vectors representing a different column in your data frame. In R, once the vectors have been created, you are able to do exactly this using data.frame().
# Build a tiny class roster data frame
names_vec <- c("John", "Bob", "Carmen", "Sarah")

ages_vec <- c(20, 22, 21, 23)

major_vec <- c("Psych", "Econ", "Psych", "CS")

roster <- data.frame(
  name = names_vec,
  age = ages_vec,
  major = major_vec,
  stringsAsFactors = FALSE)

roster

class(roster) # Class is now data.frame
name age major
1   John  20 Psych
2    Bob  22  Econ
3 Carmen  21 Psych
4  Sarah  23    CS
[1] "data.frame"
The data frame roster was created using three different vectors: names_vec, ages_vec, and major_vec. Now, we have an example of both numeric and character values together.

Subsection 1.6.2 Functions to Explore Datasets

When exploring datasets, there are some great functions to do this:
Thankfully, R comes preinstalled with datasets to explore and practice R with. Below we will test these functions out using the dataset iris.
# iris
head(iris) # By default returns 6 rows

tail(iris) # By default returns 6 rows

str(iris) # Provides the structure of each column

summary(iris) # Provides statistics for each column

colnames(iris) # Provides the column names of your data
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
145          6.7         3.3          5.7         2.5 virginica
146          6.7         3.0          5.2         2.3 virginica
147          6.3         2.5          5.0         1.9 virginica
148          6.5         3.0          5.2         2.0 virginica
149          6.2         3.4          5.4         2.3 virginica
150          5.9         3.0          5.1         1.8 virginica
'data.frame':	150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
 Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
 Median :5.800   Median :3.000   Median :4.350   Median :1.300  
 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
       Species  
 setosa    :50  
 versicolor:50  
 virginica :50  
                
                
                
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
These are by far not the only ones that can or should be used. Of course, experiment with other commands to see what works best for your style and data.

Subsection 1.6.3 Working With Columns Within Data Frames

When we created the roster data frame, it was three columns: name, age, and major. Let’s say we wanted to find the median of age in our roster data frame. How can we find the median of just the age column?
In R, to select specific columns in data frames, we can use $. The formula to follow is:
dataset_name$column_name
Below is an example of finding the median of the age column in the roster dataframe.
median(roster$age)
[1] 21.5
Just like in real life, the dollar sign ($) is very powerful. Now only can we use it to call columns from a dataset, but we can also use it to make columns in a dataset that do not exist yet. The formula to follow is:
dataset_name$new_column_name <- new_data
Below are examples of creating a new column in a data frame using the $.
# Having one value for each row of data
roster$year <- 2025

# Having a different value for each row of data
roster$minor <- c("Chemistry", "Biology", "History", "Art")

roster
name age major year     minor
1   John  20 Psych 2025 Chemistry
2    Bob  22  Econ 2025   Biology
3 Carmen  21 Psych 2025   History
4  Sarah  23    CS 2025       Art