Memra

Equal-length columns, $, nrow, dim, names

◈ 10 cards

data.frame(order_id = 1001:1004, prov = prov4, amount = amount4) is a sheet: named columns of equal length, row names 1–4 down the left. mini$amount is the vector 420 180 760 250; mini["amount"] is still a data frame; dim(mini) is 4 3, rows first.

A data frame is the sheet

Module 9 worked on single vectors. A spreadsheet is several columns side by side, each with a header, all the same length. R's version is the data frame: a set of named vectors of equal length.

Worked example — build one, print it

> mini <- data.frame(order_id = 1001:1004, prov = prov4, amount = amount4)
> mini
  order_id prov amount
1     1001   ON    420
2     1002   QC    180
3     1003   ON    760
4     1004   BC    250

Each argument to data.frame() is name = vector. The header row is the column names. Down the left is an unlabelled column of integers — the row names, 1 to 4. They are not data. Sheets shows the same thing: row numbers in grey to the left of column A.

The columns must be the same length. Three provinces for four order numbers is refused:

> data.frame(order_id = 1001:1004, prov = c("ON", "QC", "ON"))
Error in data.frame(order_id = 1001:1004, prov = c("ON", "QC", "ON")) : 
  arguments imply differing number of rows: 4, 3

$ pulls one column out as a vector

> mini$amount
[1] 420 180 760 250

mini$amount is exactly the amount4 vector from Module 9, so everything from that module applies: mean(mini$amount), miniamount > 300. The $ operator is the bridge between the sheet and the vector tools.

Size and names

> nrow(mini)
[1] 4
> ncol(mini)
[1] 3
> dim(mini)
[1] 4 3
> names(mini)
[1] "order_id" "prov"     "amount"  

dim() is rows, then columns — the str() line in L10.4 says it the same way, 4 obs. of 3 variables. One thing to unlearn: length(mini) is 3, the number of columns, because a data frame is a list of columns. Rows are nrow().

[ ] keeps the frame; [[ ]] and $ return the vector

> mini["amount"]
  amount
1    420
2    180
3    760
4    250
> mini[["amount"]]
[1] 420 180 760 250

Single brackets with a name return a one-column data frame — still printed with a header and row names. Double brackets, like $, return the bare vector. class(mini["amount"]) is "data.frame"; class(mini[["amount"]]) is "numeric". When a function wants a vector (mean, sum, table), use $.

Type the build, the column, and the dimensions

Three snippets, then the worksheet reads nrow() and ncol() off the full 12-row orders frame — six columns: order_id, prov, channel, amount, units, order_date.

CRISP-DM: building the frame is data understanding → describe data — the R form of counting rows and columns in the data dictionary.

order_idprovamount11001ON42021002QC18031003ON76041004BC250mini$amount is the highlighted column as a vector: [1] 420 180 760 250. dim(mini) is 43.
A printed data frame. The muted left column is the row names — identifiers R adds, not a variable. The header row is names(mini); the body is three equal-length vectors.
NORMAL ~/memra/learn/afm-112/data-frames-a-sheet-in-r utf-8 LF