Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 39 ยท Data Frames and Data Wrangling

Data Frames in R

Learn R data frames, the standard table for analysis. Build them with data.frame(), inspect with str() and summary(), add columns and rows, sort with order() and meet tibbles.

A data frame is R's table: rows are observations (people, orders, measurements) and columns are variables (name, age, price). Each column is a vector with one type, but different columns can have different types. That makes a data frame the right shape for nearly all real data, from a survey to a sales ledger. Almost every statistical function and plotting package in R expects one.

A data frame is really a list of equal-length vectors, which explains much of its behavior: $ selects a column just as it selects a list element, and length(df) is the number of columns.

Creating a data frame

R
people <- data.frame(
  name   = c("Ana", "Ben", "Cleo"),
  age    = c(28, 35, 41),
  member = c(TRUE, FALSE, TRUE)
)
people
#>   name age member
#> 1  Ana  28   TRUE
#> 2  Ben  35  FALSE
#> 3 Cleo  41   TRUE

Every column must be the same length (R recycles shorter ones only when the lengths divide evenly, and otherwise raises an error). The numbers on the left are row names; by default they are just 1, 2, 3.

Since R 4.0, text columns stay as character vectors. Make a category column a factor explicitly when you want one.

Inspecting a data frame

Always look before you compute. These functions answer "what do I have?":

R
dim(people)
#> [1] 3 3

nrow(people)
#> [1] 3

names(people)        # also colnames(people)
#> [1] "name"   "age"    "member"

str(people)
#> 'data.frame':	3 obs. of  3 variables:
#>  $ name  : chr  "Ana" "Ben" "Cleo"
#>  $ age   : num  28 35 41
#>  $ member: logi  TRUE FALSE TRUE

str() shows each column's type and first values. It is the single most useful check after importing data. Others:

  • head(df, n) and tail(df, n) show the first or last rows (6 by default).
  • summary(df) gives per-column statistics.
  • View(df) opens a spreadsheet-like viewer in RStudio.
R
summary(people$age)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   28.00   31.50   35.00   34.67   38.00   41.00 

A built-in example with more rows is the iris flower measurements:

R
head(iris, 3)
#>   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

dim(iris)
#> [1] 150   5

Working with columns

A column is a vector, so everything from earlier lessons applies:

R
people$age
#> [1] 28 35 41

mean(people$age)
#> [1] 34.66667

people$age > 30
#> [1] FALSE  TRUE  TRUE

Add a column by assigning to a new name, and remove one by assigning NULL:

R
people$senior <- people$age > 30
people
#>   name age member senior
#> 1  Ana  28   TRUE  FALSE
#> 2  Ben  35  FALSE   TRUE
#> 3 Cleo  41   TRUE   TRUE

people$senior <- NULL

Rename columns by assigning to names(): names(people)[2] <- "years".

flowchart LR
    subgraph DF["data frame"]
        A["name<br/>character"]
        B["age<br/>numeric"]
        C["member<br/>logical"]
    end
    DF --> R["each row = one observation"]

Figure: columns are typed vectors; a row is one observation across them.

Adding rows

Stack data frames with rbind(). The new rows must have the same column names:

R
extra <- data.frame(name = "Dev", age = 22, member = FALSE)
people <- rbind(people, extra)
nrow(people)
#> [1] 4

Growing a data frame one row at a time inside a loop is slow. Collect pieces in a list and combine them once with do.call(rbind, pieces).

Sorting rows

order() returns the row positions that would sort a column. Use it to index the rows:

R
people[order(people$age), ]            # ascending
people[order(people$age, decreasing = TRUE), ]
people[order(people$member, -people$age), ]   # two keys

The comma matters: df[rows, columns], with the columns part empty to keep all of them. Row and column selection is the subject of Subsetting Data Frames.

Useful everyday functions

Task Code
Unique values of a column unique(df$col)
Count each value table(df$col)
Column means colMeans(df[ , c("a", "b")])
Apply to every column sapply(df, class)
Remove duplicate rows df[!duplicated(df), ]
Check for missing values colSums(is.na(df))
Convert a column type df$x <- as.numeric(df$x)

sapply(df, class) is a quick audit of column types.

Tibbles: the tidyverse data frame

The tibble package provides a modern variant that prints more sensibly and fails sooner on mistakes:

R
library(tibble)

tb <- tibble(
  name = c("Ana", "Ben"),
  age  = c(28, 35)
)
tb
#> # A tibble: 2 ร— 2
#>   name    age
#>   <chr> <dbl>
#> 1 Ana      28
#> 2 Ben      35

A tibble shows the column types under the names, prints only the first rows of a large table, never converts text to factors and does not partially match column names. Every tibble is also a data frame, so everything in this lesson still works. The tidyverse packages in dplyr Basics return tibbles.

Common mistakes

  1. Expecting df[1] to behave like df[[1]]: the first returns a one-column data frame, the second the column as a vector.
  2. Columns read as the wrong type (numbers stored as text) because of a stray character in the file.
  3. Forgetting that df$col with a misspelled name quietly returns NULL.
  4. Using rbind() with different column names or order of types.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a data frame, structurally?

    Columns are vectors that may differ in type, but all have the same length.
  2. 2. Which function shows the type and first values of every column?

    str() is the standard first check after creating or importing a data frame.
  3. 3. How do you add a column total that is a + b?

    Assigning to a new name with $ creates the column.
  4. 4. Which sorts the rows of df by score, highest first?

    order() gives row positions; index the rows with them and leave the column part empty.

Summary

  • A data frame is a table of equal-length, individually typed columns; build one with data.frame().
  • Inspect with str(), dim(), head() and summary().
  • Select a column with $; add one by assignment; remove one with NULL.
  • Add rows with rbind(), sort with df[order(...), ].
  • Tibbles are friendlier data frames used throughout the tidyverse.

Next: Reading and Writing Data