AnyCompiler

Data Frames

A data frame is R's central tabular data structure — the reason R exists at all. Underneath, it's simply a named list of vectors, all the same length, which R displays and indexes as a 2D table: each named vector becomes a column, and position i across all of them becomes row i. data.frame() builds one from vectors you already have; $ reaches into a named column exactly the way it reached into a named list field; nrow() and ncol() report its shape.

df <- data.frame(name = c("Ada", "Alan"), age = c(36, 41))
print(df)
print(df$name)
print(nrow(df))
print(ncol(df))
Output
  name age
1  Ada  36
2 Alan  41
[1] "Ada"  "Alan"
[1] 2
[1] 2

Printing df shows a proper aligned table, with row numbers down the left and column names across the top — R built that formatting for you from nothing more than two plain vectors of the same length. df$name pulled the name column back out as an ordinary character vector, the same type it went in as. Next: data frame manipulation — filtering rows and adding a computed column.

Practice exercise

Create df <- data.frame(item = c("pen", "book"), price = c(1.5, 12)). Print nrow(df), then print df$item.