R Data Frames

A data frame is the most important data structure in R for real-world data analysis. It stores data in a tabular format — rows and columns — where each column can hold a different data type. If you have ever used a spreadsheet or a database table, a data frame works the same way.

What Is a Data Frame?

A data frame is a table where:
  → Each column is a vector of one type
  → All columns have the same number of rows
  → Columns have names; rows can have names

   Name (chr)   Age (num)   Score (num)   Passed (lgl)
   ──────────────────────────────────────────────────────
   "Anita"       22          88            TRUE
   "Ravi"        25          72            TRUE
   "Seema"       21          45            FALSE
   "Kiran"       23          95            TRUE

Creating a Data Frame

students <- data.frame(
  name   = c("Anita", "Ravi", "Seema", "Kiran"),
  age    = c(22, 25, 21, 23),
  score  = c(88, 72, 45, 95),
  passed = c(TRUE, TRUE, FALSE, TRUE)
)

print(students)

Inspecting a Data Frame

nrow(students)      # 4 (number of rows)
ncol(students)      # 4 (number of columns)
dim(students)       # 4 4
names(students)     # "name" "age" "score" "passed"
str(students)       # compact structure with types
head(students, 3)   # first 3 rows
tail(students, 2)   # last 2 rows
summary(students)   # statistics for each column

Accessing Data Frame Elements

# Access a column (returns a vector)
students$name          # "Anita" "Ravi" "Seema" "Kiran"
students[["score"]]    # 88 72 45 95
students[, "age"]      # 22 25 21 23
students[, 2]          # same as above (column index)

# Access a specific row
students[1, ]          # first row (all columns)
students[2:3, ]        # rows 2 and 3

# Access a specific cell
students[1, "score"]   # 88
students[3, 3]         # 45

Filtering Rows

# Students who passed
students[students$passed == TRUE, ]

# Students with score above 70
students[students$score > 70, ]

# Students aged 22 or younger
students[students$age <= 22, ]

Adding Columns

# Add a grade column
students$grade <- ifelse(students$score >= 75, "A", 
                  ifelse(students$score >= 50, "B", "C"))

# Add a bonus score column
students$bonus_score <- students$score + 5

print(students)

Adding Rows

new_student <- data.frame(
  name   = "Dev",
  age    = 24,
  score  = 80,
  passed = TRUE
)

students <- rbind(students, new_student)

Removing Columns

students$bonus_score <- NULL   # remove a column

# Keep only specific columns
students_slim <- students[, c("name", "score")]

Reading Data from a CSV File

# Real data frames usually come from files
data <- read.csv("students.csv")
str(data)

Creating a Data Frame from Vectors

ids     <- 101:105
cities  <- c("Delhi","Mumbai","Pune","Jaipur","Surat")
sales   <- c(4500, 6200, 3800, 5100, 4900)

sales_df <- data.frame(id = ids, city = cities, monthly_sales = sales)
print(sales_df)

Output:

   id    city   monthly_sales
1 101   Delhi           4500
2 102  Mumbai           6200
3 103    Pune           3800
4 104  Jaipur           5100
5 105   Surat           4900

Data frames are where real data analysis in R lives. Every time you load a dataset — CSV, Excel, database query — it becomes a data frame. Every data manipulation, visualization, and statistical test operates on data frames. Mastering data frames is the single most important skill in R.

Leave a Comment

Your email address will not be published. Required fields are marked *