Chapter 3: put names and heights into an R data frame
How do I put names and heights into a table in R?
Use data.frame() to put 6 seedling names and 6 heights into a table. Each row holds one seedling; each column holds one kind of information.
On this page
In chapter 2, we gave each height a name. I now want to see the names and heights side by side. We will build the small table below, take a value from it and use its columns to draw this graph.

What are a row, a column and a data frame?
A row runs across a table; a column runs down it. Here, one row contains everything we record about one seedling. The first column holds its name, and the second holds its height in centimetres. R calls this kind of table a data frame.
A vector is a sequence of values, as in chapter 2. We put two vectors together as two columns: six text names and six numbers. Each column has six entries. Data type describes how R stores a value; the name column uses text, and the height column uses numbers.
| Seedling | Height (cm) |
|---|---|
| A | 8 |
| B | 11 |
| C | 9 |
| D | 14 |
| E | 12 |
| F | 18 |
This article uses simulated example data. In RStudio, choose File → New File → R Script and save the code file as table.R. Select each short block below and click Run. Read the text results in Console; the graph will appear in Plots.
How do I create the six-row table?
data.frame() joins the named columns in the order you supply them. Save the result as seedlings.
A function is a named operation, such as data.frame(). Its parentheses contain the inputs. Inside them, seedling = gives the first column its name; height_cm = names the second. c() joins values into a vector, commas separate them, and straight double quotes mark text such as "A". The arrow <- saves the whole table under the name seedlings. print() displays it.
Input
seedlings <- data.frame(
seedling = c("A", "B", "C", "D", "E", "F"),
height_cm = c(8, 11, 9, 14, 12, 18)
)
print(seedlings)Output
seedling height_cm
1 A 8
2 B 11
3 C 9
4 D 14
5 E 12
6 F 18Read one row across: row 3 says C and 9. The small numbers at the far left are row labels that R adds. Our table has two data columns, with headings seedling and height_cm. Keep the six names and six heights in the same order so each name stays paired with its height.
How do I check the number of rows and columns?
nrow() counts rows, ncol() counts columns, and names() shows the column names. You should see 6, 2, then the two names.
In these outputs, [1] marks the first value on the printed line. Check the two column names carefully: later commands use exactly these spellings, including the underscore in height_cm.
Input
nrow(seedlings)
ncol(seedlings)
names(seedlings)Output
[1] 6
[1] 2
[1] "seedling" "height_cm"How do I take the height column out of the table?
seedlings$height_cm selects the height column as a numeric vector. Its six values have a mean of 12 cm.
Read $ as “the column named”. Write the table name on its left and the full column name on its right. mean() calculates the average: add the six heights to get 72, then divide by 6. Here, the parentheses pass the selected heights to mean().
Input
seedlings$height_cm
mean(seedlings$height_cm)Output
[1] 8 11 9 14 12 18
[1] 12How do I read one row or one cell?
seedlings[3, ] gives row 3. seedlings[3, "height_cm"] gives its height, 9 cm.
A cell is where a row and a column meet. Inside these square brackets, the part before the comma selects rows and the part after it selects columns. Leaving the column part empty keeps all columns. The second command supplies a quoted column name to choose a single cell.
Input
seedlings[3, ]
seedlings[3, "height_cm"]Output
seedling height_cm
3 C 9
[1] 9How do I draw a graph from those two columns?
Pass the heights to barplot() and the names to names.arg. Each bar then keeps the label from the same row.
barplot() draws bars. names.arg sets the words beneath them. The other settings are arguments, meaning inputs that control the operation: col sets blue, border = NA omits the outlines, and ylim = c(0, 20) sets the vertical scale from 0 to 20. NA is R’s missing-value marker; this border setting uses it to omit an outline. xlab and ylab name the horizontal and vertical axes; main supplies the graph title.
Input
barplot(seedlings$height_cm,
names.arg = seedlings$seedling,
col = "#0066CC", border = NA, ylim = c(0, 20),
xlab = "Seedling", ylab = "Height (cm)",
main = "Heights from a data frame")I check A and F first: their bars should reach 8 and 18 cm. Then I compare the four middle bars with the table. If the names and heights look mismatched, return to the two vectors and check their order.
Can you take rows 2 and 5 and check their heights?
Rows 2 and 5 contain B at 11 cm and E at 12 cm. Their total is 23 cm and their average is 11.5 cm.
Try saving those two rows as practice. In the row position inside [ ], c(2, 5) selects both positions; keep the comma and leave the column position empty to keep both columns. sum() adds the selected heights, and mean() divides that total by the number of heights. Open the answer when you are ready to compare.
Show the worked answer
Input
practice <- seedlings[c(2, 5), ]
print(practice)
sum(practice$height_cm)
mean(practice$height_cm)Output
seedling height_cm
2 B 11
5 E 12
[1] 23
[1] 11.5If you can point to a row, name its two columns and find the height for C, you have the main idea. You can come back to the comma example whenever you need it. Chapter 4 is planned to open a saved table file and inspect its contents.
How can I download and run the complete example?
Download lesson.R for the lesson code. Save export.R in the same folder to create the PNG, PDF and two CSV tables.
A working directory is the folder where R looks for and saves files. In RStudio, choose Session → Set Working Directory → Choose Directory and select that folder, then run source("export.R"). source() runs the commands saved in the named file. PNG is a picture format; pixels are its tiny picture dots. PDF is a document format, and CSV stores table columns separated by commas. The PNG is 1200 × 800 pixels; the PDF is 7.5 × 5 inches. Tested with R 4.5.3 and the tools supplied with R, grouped into packages.
Where can I read the official details?
The R help pages describe how data.frame() creates columns and how $ and square brackets select their contents. This lesson checks those operations on the six-row example.
Learn R

