Data Frame (R)
A data frame is R's table structure: a list of equal-length columns where each column is a vector and can hold a different type. It is the standard format for rectangular, spreadsheet-like data.
R
A data frame stores data in rows and columns, like a spreadsheet or database table. Each column is a vector of a single type (numeric, character, factor, logical), but different columns can have different types — which distinguishes it from a matrix, where everything is one type.
You create one with data.frame(), read one from a file with read.csv(), and inspect it with str(), head(), summary(), nrow(), and ncol(). Access a column with df$name or df[["name"]], and subset rows/columns with df[rows, cols].
Data frames are the workhorse of data analysis in R and the input to almost every modeling and plotting function. The tidyverse offers the tibble as a modern, friendlier variant, and dplyr provides verbs (filter, mutate, summarise) for transforming them.
Syntax
df <- data.frame(name = c("a", "b"), age = c(30, 25))Examples
R
df <- data.frame(
name = c("Ana", "Sam"),
age = c(30, 25)
)
print(df$age)
print(nrow(df))Output:
[1] 30 25
[1] 2