đ Introduction
A data frame is one of the most important data structures in R. It is a two-dimensional table consisting of rows and columns, where each column can store a different data type. Data frames are widely used in data analysis, statistics, machine learning, and data visualization because they closely resemble spreadsheet tables and database tables.
Information
đ¯ Why Use Data Frames?
- Store structured tabular data.
- Allow different data types in different columns.
- Simplify data manipulation and analysis.
- Serve as the primary data structure for statistical computing.
- Integrate seamlessly with R packages such as dplyr and ggplot2.
đĻ Creating a Data Frame
The data.frame() function is used to create a data frame. Each column is defined as a vector, and all columns must have the same number of elements.
Creating a Data Frame
students <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(21, 22, 20),
Marks = c(85, 90, 88)
)
print(students)Output
Console Output
Name Age Marks
1 Alice 21 85
2 Bob 22 90
3 Charlie 20 88đ Structure of a Data Frame
đ Viewing Data Frame Information
| Function | Description | Example |
|---|---|---|
| str() | Displays the structure. | str(df) |
| summary() | Displays summary statistics. | summary(df) |
| head() | Shows the first six rows. | head(df) |
| tail() | Shows the last six rows. | tail(df) |
| dim() | Returns the dimensions. | dim(df) |
| names() | Returns column names. | names(df) |
Viewing Data Frame Information
students <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(21,22,20),
Marks = c(85,90,88)
)
str(students)
summary(students)
head(students)
tail(students)
dim(students)
names(students)đ¯ Accessing Data
Data in a data frame can be accessed by column name, row and column indexes, or the $ operator.
Using $
students$Name
students$MarksUsing Indexes
students[2,3]
students[1,]
students[,2]Using Column Names
students[, "Marks"]
students[, c("Name","Age")]âī¸ Adding Columns
A new column can be added by assigning values to a new column name.
Adding a Column
students$Grade <- c("A", "A+", "A")
print(students)â Adding Rows
Use the rbind() function to append a new row.
Adding a Row
students <- data.frame(
Name = c("Alice", "Bob"),
Age = c(21,22),
Marks = c(85,90)
)
new_student <- data.frame(
Name = "Charlie",
Age = 20,
Marks = 88
)
students <- rbind(students, new_student)
print(students)â Removing Columns
Removing a Column
students$Marks <- NULL
print(students)â Removing Rows
Removing a Row
students <- students[-2, ]
print(students)đ Updating Data
Updating Values
students$Marks[1] <- 95
students[2, "Age"] <- 23
print(students)đ Selecting Rows Using Conditions
Rows can be filtered using logical conditions.
Filtering Data
students <- data.frame(
Name = c("Alice","Bob","Charlie"),
Marks = c(85,90,75)
)
students[students$Marks >= 85, ]đ Useful Data Frame Functions
| Function | Description |
|---|---|
| nrow() | Returns the number of rows. |
| ncol() | Returns the number of columns. |
| dim() | Returns dimensions. |
| names() | Returns column names. |
| str() | Displays the structure. |
| summary() | Displays summary statistics. |
Useful Functions
print(nrow(students))
print(ncol(students))
print(dim(students))
print(names(students))
summary(students)đ§ Data Frame Workflow
đ Real-World Example
The following data frame stores employee information for a company.
Employee Data
employees <- data.frame(
ID = c(101,102,103),
Name = c("John","Sophia","David"),
Department = c("HR","Finance","IT"),
Salary = c(45000,62000,58000),
Active = c(TRUE, TRUE, FALSE)
)
print(employees)
summary(employees)
employees[employees$Salary > 50000, ]đ Matrix vs Data Frame
| Feature | Matrix | Data Frame |
|---|---|---|
| Dimensions | Two-dimensional | Two-dimensional |
| Data Types | Single type only | Different types per column |
| Rows and Columns | Yes | Yes |
| Primary Use | Mathematical computations | Tabular data analysis |
â ī¸ Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| Columns with unequal lengths | All columns must contain the same number of rows. | Ensure every column has equal length. |
| Using invalid column names | Misspelled names produce errors. | Check column names using names(). |
| Removing important columns accidentally | Assigning NULL deletes the column. | Verify before removing data. |
| Confusing matrices with data frames | Matrices store only one data type. | Use data frames when columns have different data types. |
đĄ Best Practices
- Use meaningful column names.
- Store related information in the same data frame.
- Inspect the structure using str() before analysis.
- Use logical conditions to filter data efficiently.
- Keep data clean by removing duplicate or unnecessary records.
Best Practice
đ Summary
Data frames are versatile two-dimensional data structures that allow each column to store a different data type while maintaining equal-length rows. You learned how to create data frames, inspect their structure, access and modify data, add or remove rows and columns, filter records, and use essential functions for data analysis. Because most real-world datasets are stored in tabular form, mastering data frames is one of the most important skills in R programming.