Skip to content
AgileLogics
7 min read

Data Frames

Prerequisites: Lists, Factors

A data frame is R's table: rows are observations (people, days, products) and columns are variables (name, age, price). Each column is a vector, so every value in a column has the same type — but different columns can have different types. It's the structure you'll use most.

Real-world analogy: a data frame is a spreadsheet, with a header row naming each column.

Creating a data frame

people <- data.frame(
  name = c("Ada", "Linus", "Grace", "Alan"),
  age = c(36, 28, 45, 41),
  city = c("London", "Helsinki", "New York", "London")
)
people
   name age     city
1   Ada  36   London
2 Linus  28 Helsinki
3 Grace  45 New York
4  Alan  41   London

Exploring a data frame

people <- data.frame(
  name = c("Ada", "Linus", "Grace", "Alan"),
  age = c(36, 28, 45, 41),
  city = c("London", "Helsinki", "New York", "London")
)
nrow(people)
ncol(people)
names(people)
str(people)
summary(people$age)
[1] 4
[1] 3
[1] "name" "age"  "city"
'data.frame':	4 obs. of  3 variables:
 $ name: chr  "Ada" "Linus" "Grace" "Alan"
 $ age : num  36 28 45 41
 $ city: chr  "London" "Helsinki" "New York" "London"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   28.0    34.0    38.5    37.5    42.0    45.0

head(df) shows the first six rows — the first thing to run on any new dataset. In RStudio, View(df) opens a spreadsheet-like viewer.

Selecting columns and rows

people <- data.frame(
  name = c("Ada", "Linus", "Grace", "Alan"),
  age = c(36, 28, 45, 41),
  city = c("London", "Helsinki", "New York", "London")
)
people$age
people[2, ]
people[, c("name", "city")]
people[people$age > 40, ]
people[people$city == "London", "name"]
[1] 36 28 45 41
   name age     city
2 Linus  28 Helsinki
   name     city
1   Ada   London
2 Linus Helsinki
3 Grace New York
4  Alan   London
   name age     city
3 Grace  45 New York
4  Alan  41   London
[1] "Ada"  "Alan"

df[rows, columns] — leave a side empty to mean "all". Logical conditions select rows.

Adding columns

people <- data.frame(name = c("Ada", "Linus"), age = c(36, 28))
people$age_in_months <- people$age * 12
people$senior <- people$age >= 35
people
   name age age_in_months senior
1   Ada  36           432   TRUE
2 Linus  28           336  FALSE

In Level 2 you'll do all of this with dplyr, which makes data frame code much more readable — but the base-R way above is worth knowing, because you'll see it everywhere.

Common mistakes

  • Forgetting the comma: people[people$age > 40] (no comma) selects columns, not rows, and gives an error or a confusing result.

Practice

Create a data frame of three products with item and price columns. Add a price_with_tax column (price × 1.2) and print only the products whose price with tax is over 10.

Show solution
products <- data.frame(item = c("pen", "book", "bag"), price = c(2, 12, 30))
products$price_with_tax <- products$price * 1.2
products[products$price_with_tax > 10, ]
  item price price_with_tax
2 book    12           14.4
3  bag    30           36.0