Australian National University · FACULTY OF STATISTICS

BIOL2001 Chap.4 Data Frames: Files, Factors, Merges and Aggregation

- one subject, every graph, every model, every mark
8 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 4 of 15 · BIOL2001

Data Frames: Files, Factors, Merges and Aggregation

Working with real tables

A data frame holds a field data sheet in R: each column can have its own type, but every value in a column shares it. This chapter follows a five-step routine for a new dataset: read the file, check the columns, clean the values, combine tables and summarise.

It explains the arguments of read.table() and write.table(), why selecting one column returns a vector, and how logical expressions select rows and replace values.

Merging and grouping

A figure computes the four merges of two tables that share a plot key, showing why inner, left, right and full joins keep different numbers of rows.

A worked example builds a factor from text, explains its alphabetical levels and integer codes, and uses aggregate() to compute a mean for each group. The chapter also covers rbind() and cbind(), subset(), names(), and why rows cannot be deleted by assigning NULL.

In this chapter

What this chapter covers

  • 01

    Data frames as heterogeneous matrices

  • 02

    Arguments of read.table and write.table

  • 03

    Selecting rows with logical expressions

  • 04

    Factors, levels and integer codes

  • 05

    Adding and deleting columns

  • 06

    Stacking tables with rbind and cbind

  • 07

    Inner, left, right and full merges

  • 08

    Filtering with subset() and grouping with aggregate()

Worked example · free

Counting the rows each merge keeps

Q [4 marks]. Table X holds soil pH for plots P1 to P4 and table Y holds species richness for plots P2 to P6, both keyed by plot. How many rows do the inner, left outer, right outer and full outer merges return, and which arguments of merge() produce each? The practice weighting is assigned by AskSia and is not an official assessment.
Two tables keyed by plot: X holds soil pH for plots P1 to P4 and Y holds species richness for P2 to P6.Two tables keyed by plot: X holds soil pH for plots P1 to P4 and Y holds species richness for P2 to P6. The four merges keep 3, 4, 5 and 6 rows, filling NA wherever one table has no matching plot.X: pHP1P2P3P4Y: richnessP2P3P4P5P6inner: 3 rowsall.x = FALSE, all.y = FALSE · P2 to P4left outer: 4 rowsall.x = TRUE · P1 to P4right outer: 5 rowsall.y = TRUE · P2 to P6full outer: 6 rowsall = TRUE · P1 to P6key column: plot
Two tables keyed by plot: X holds soil pH for plots P1 to P4 and Y holds species richness for P2 to P6. The four merges keep 3, 4, 5 and 6 rows, filling NA wherever one table has no matching plot.
  • 1Inner join keeps plots in both tables, P2 to P4: 3 rows, with all.x and all.y FALSE.
  • 1Left outer join keeps every plot of X: 4 rows, with all.x = TRUE and NA richness for P1.
  • 1Right outer join keeps every plot of Y: 5 rows, with all.y = TRUE and NA pH for P5 and P6.
  • 1Full outer join keeps every plot from either table: 6 rows, with all = TRUE.
The merges return 3, 4, 5 and 6 rows. All four come from merge(X, Y, by = "plot") with different all.x and all.y settings, and every unmatched plot receives NA in the columns from the other table.
Sia tip — Count the rows straight after every merge and compare them with the number you expected; an unexpected count usually means a key is spelled differently in the two files.
Glossary

Key terms

Key Column
A column shared by two data frames that merge() uses to match their rows.
Inner Join
A merge that keeps only the rows whose key appears in both tables.
Factor Level
One of the distinct categories of a factor, stored internally as an integer code.
Left Outer Join
A merge that keeps every row of the first table and adds matching values from the second.
Aggregation Function
A function such as mean, median, sum or sd that summarises many values into one.
FAQ

Data Frames: Files, Factors, Merges and Aggregation FAQ

What does stringsAsFactors = FALSE do when reading a file?

It keeps text columns as character vectors instead of converting them into factors. Factors suit grouping variables such as sex or habitat, but not free text or names you may edit, so reading text as text and converting chosen columns later keeps control in your hands.

Why are factor levels in alphabetical order?

R assigns integer codes to the distinct values of a factor in alphabetical order by default. For a factor made from male and female, female becomes level 1 and male level 2, even if the first record is male, which also makes female the reference group in plots and models.

How do I delete a row from a data frame?

You cannot delete a row by assigning NULL, as you can for a column. Instead, build a new data frame without that row, for example by indexing with a negative position or a logical condition, and assign it back to the same name.

What does aggregate(len ~ sex, d, mean) return?

It returns a small data frame with one row for each level of sex and the mean of len for that group. The formula reads as summarise len for each value of sex, and any summary function, such as median or sd, can replace mean.

Study strategy

Assessment move

Download one workshop data file and run the five-step routine on it from start to finish, writing a comment above each step. Build two small tables that share a key, predict the row count of all four merges, then check them in RStudio. Finally convert a text column to a factor and use aggregate() to summarise another column by it.

Keep a short checklist beside you for every new file: separator, header row, text or factor columns, row count after each merge, and a final write.table() with row names switched off.

Working through Data Frames: Files, Factors, Merges and Aggregation in BIOL2001? Sia is AskSia’s AI Statistics tutor — ask any BIOL2001 Data Frames: Files, Factors, Merges and Aggregation question and get a clear, step-by-step explanation grounded in how BIOL2001 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 46 of your Australian National University subjects - and 1,000+ Bibles across every Australian university.
Sia - your BIOL2001 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works