BIOL2001 Chap.4 Data Frames: Files, Factors, Merges and Aggregation
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.
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
- 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.
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.
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.
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.
STAT1008 Quantitative Research Methods · BIOL1030 Diversity of Life · STAT7038 Regression Modelling