Learn R Programming  /  Data Structures  /  Data Frame

Data Frame

A data frame is a table, just like a spreadsheet. Every column can hold a different type, and this is the structure you will use for almost every real dataset in R.

Creating a Data Frame

R Code
df <- data.frame(
  name  = c("Rafi", "Meera", "Zayn"),
  age   = c(23, 25, 21),
  score = c(88, 92, 76)
)
df
Output
   name age score
1  Rafi  23    88
2 Meera  25    92
3  Zayn  21    76

Exploring a Data Frame

These functions are the first thing to run on any new dataset.

R Code
str(df)      # structure: column types and a preview
dim(df)      # rows, columns
nrow(df); ncol(df)
names(df)    # column names
head(df, 2)  # first 2 rows
summary(df)  # quick statistical summary per column
Output
'data.frame':	3 obs. of  3 variables:
 $ name : chr  "Rafi" "Meera" "Zayn"
 $ age  : num  23 25 21
 $ score: num  88 92 76
[1] 3 3
[1] 3
[1] 3
[1] "name"  "age"   "score"
   name age score
1  Rafi  23    88
2 Meera  25    92

Selecting Columns

R Code
df$name          # one column, as a vector
df[, "age"]      # same, using bracket notation
df[, c("name","score")]  # multiple columns, stays a data frame
Output
[1] "Rafi"  "Meera" "Zayn" 
[1] 23 25 21
   name score
1  Rafi    88
2 Meera    92
3  Zayn    76

Selecting Rows & Filtering

This is the single most-used data frame operation: keep only the rows that meet a condition.

R Code
df[1, ]              # first row, all columns
df[df$score > 80, ]   # rows where score > 80
subset(df, age < 25)  # subset() reads a little more naturally
Output
  name age score
1 Rafi  23    88
   name age score
1  Rafi  23    88
2 Meera  25    92
   name age score
1  Rafi  23    88
3  Zayn  21    76

Adding & Modifying Columns

R Code
df$passed <- df$score >= 80       # new logical column
df$grade  <- ifelse(df$score >= 90, "A", "B")
df
Output
   name age score passed grade
1  Rafi  23    88   TRUE     B
2 Meera  25    92   TRUE     A
3  Zayn  21    76  FALSE     B

Sorting a Data Frame

R Code
df[order(df$score), ]              # ascending
df[order(-df$score), ]             # descending
Output
   name age score passed grade
3  Zayn  21    76  FALSE     B
1  Rafi  23    88   TRUE     B
2 Meera  25    92   TRUE     A
   name age score passed grade
2 Meera  25    92   TRUE     A
1  Rafi  23    88   TRUE     B
3  Zayn  21    76  FALSE     B

Advanced: Grouped Summaries with aggregate()

🚀 Advanced Example
Base R can compute group-wise statistics without any extra packages using aggregate() - similar to a GROUP BY in SQL.
R Code
df2 <- data.frame(
  dept  = c("Sales","Sales","IT","IT"),
  salary = c(50000, 55000, 60000, 65000)
)

aggregate(salary ~ dept, data = df2, FUN = mean)
Output
  dept salary
1   IT  62500
2 Sales 52500

Key Points

  • A data frame is a table: rows are observations, columns are variables, each column has its own type.
  • Use str(), dim(), and summary() to explore a new dataset before doing anything else.
  • Filter rows with a logical condition inside df[condition, ] or with subset().
  • aggregate() computes group-wise summaries without needing any extra package.