If matrices are R's version of a grid, data frames are R's version of a spreadsheet. And honestly, data frames are the most important data structure in R. Almost every real-world analysis starts with loading data into a data frame. Each column can be a different type โ numbers in one, text in another, dates in a third. That's what makes them so powerful.
Creating a Data Frame with data.frame()
Pass named vectors to data.frame(). Each vector becomes a column. Unlike matrices, each column can hold a different data type. This is the structure you'll use for surveys, experiments, sales records โ basically any tabular data.
students <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(22, 19, 24),
grade = c("A", "B", "A")
)
print(students)
Try it Yourself โ
Peeking at Your Data: str(), head(), summary()
Real datasets can be huge. Before diving in, use a few essential functions. str() shows the structure โ column names, types, and a preview. head() shows the first few rows. summary() gives you statistics for every column.
students <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(22, 19, 24),
grade = c("A", "B", "A")
)
# Structure
str(students)
# First few rows
head(students)
# Summary statistics
summary(students)
Try it Yourself โ