If you are learning R, one of the first things you will come across is the data frame. Data frames are one of the most useful ways to store and work with data in R. Whether you are looking at student marks, customer details, sales records, employee information, or survey answers, a data frame can help you keep everything organized.
The good news is that data frames are not as difficult as they may sound. You can think of a data frame as a simple table, much like a table you might create in Excel. It has rows and columns, and each column usually represents one type of information.
In this guide, we will understand what a data frame is, how to create one in R, how to view its information, how to select rows and columns, and how to make simple changes. The examples are kept simple so that beginners can follow along without needing advanced R knowledge.
What Is a Data Frame in R?
A data frame is a table used to store data in R. Each row represents one record, while each column represents a particular piece of information.
Imagine you have information about four students. You may want to store their names, ages, and marks. In a normal table, the information could look like this:
| Name | Age | Marks |
| Rahul | 20 | 85 |
| Priya | 21 | 91 |
| Amit | 19 | 78 |
| Neha | 20 | 88 |
This type of table can be represented as a data frame in R.
The Name column contains names, the Age column contains numbers, and the Marks column also contains numbers. Together, these columns form one data frame.
Data frames are especially useful because they allow you to keep different kinds of information together. A person’s name can be text while their age is a number, and both can exist in the same table.
Creating Your First Data Frame
R provides a simple function called data.frame() for creating a data frame.
Here is a basic example:
students <- data.frame(
Name = c(“Rahul”, “Priya”, “Amit”, “Neha”),
Age = c(20, 21, 19, 20),
Marks = c(85, 91, 78, 88)
)
The name students is given to the data frame. Inside data.frame(), we create three columns: Name, Age, and Marks.
The c() function puts several values together. For example:
c(“Rahul”, “Priya”, “Amit”, “Neha”)
creates a group of four names.
After running the code, you can simply type:
students
R will show the table.
The result will look similar to this:
Name Age Marks
1 Rahul 20 85
2 Priya 21 91
3 Amit 19 78
4 Neha 20 88
The numbers on the left are row numbers. They help you identify individual rows.
Understanding Rows and Columns
It is important to understand the basic structure of a data frame.
In our example, there are four rows because we have information about four students. There are three columns because we have three types of information: names, ages, and marks.
You can use the nrow() function to find the number of rows.
nrow(students)
The result is:
4
To find the number of columns, use:
ncol(students)
The result is:
3
You can also use dim() to see both numbers at the same time.
dim(students)
The result will be:
4 3
This means the data frame has four rows and three columns.
Looking at the Data Frame
When a data frame contains a small amount of information, typing its name is enough to see everything.
students
But real data can contain hundreds or thousands of rows. You usually do not want to display everything at once.
The head() function lets you see the first few rows.
head(students)
By default, it shows the first six rows. Since our example has only four rows, all four will be shown.
You can also tell R how many rows you want to see.
head(students, 2)
This shows the first two rows.
There is also a tail() function, which shows the last few rows.
tail(students, 2)
This can be helpful when you want to quickly check the beginning or end of a larger data frame.
Finding the Names of Columns
Sometimes you may receive a data frame from another person or load data from a file and want to know what columns it contains.
The names() function can help:
names(students)
The result will be:
“Name” “Age” “Marks”
This is a simple way to check the column names before working with the data.
You can also use str() to get a quick look at how the data frame is organized.
str(students)
For beginners, the output may look a little confusing at first, but the main idea is simple. It tells you the column names, the type of information stored in each column, and some example values.
Selecting One Column
One of the most common things you will do with a data frame is select a column.
Suppose you only want to see the names of the students. You can write:
students$Name
The result is:
“Rahul” “Priya” “Amit” “Neha”
Similarly, you can get the marks by writing:
students$Marks
This gives:
85 91 78 88
The $ symbol is a simple way to access a column by its name.
You can also select a column using brackets:
students[, “Name”]
Both methods are useful, but students$Name is often easier for beginners to understand.
Selecting a Specific Row
You can also select individual rows.
For example:
students[1, ]
This selects the first row.
The result is:
Name Age Marks
1 Rahul 20 85
The comma is important here. Everything before the comma refers to rows, while everything after it refers to columns.
So:
students[1, ]
means the first row and all columns.
To select the second row:
students[2, ]
You can also select more than one row:
students[1:2, ]
This gives you the first two rows.
Selecting Rows and Columns Together
You can select a particular row and column at the same time.
For example:
students[1, 3]
This means the first row and third column. The result is:
85
You can also select the first two rows and the first two columns:
students[1:2, 1:2]
The result will contain Rahul and Priya along with their ages.
This row-and-column approach is useful when you want to work with only a small part of a larger table.
Adding a New Column
Data frames can be changed after they are created.
Suppose you want to add a column showing whether each student passed. You could write:
students$Passed <- c(TRUE, TRUE, TRUE, TRUE)
Now the data frame contains another column called Passed.
You can also create a new column based on existing information.
For example, suppose students who scored 80 or more should be marked as “Good”:
students$Result <- ifelse(students$Marks >= 80, “Good”, “Needs Improvement”)
Now R checks each student’s marks. If the marks are 80 or higher, it adds “Good”. Otherwise, it adds “Needs Improvement”.
The data frame will now contain the new Result column.
Changing Values in a Data Frame
You can change information in a data frame just as easily.
Suppose Amit’s marks were entered incorrectly and should be 82 instead of 78.
You can change the value with:
students$Marks[3] <- 82
Because Amit is in the third row, R changes the third value in the Marks column.
You can then check the data frame again:
students
You should now see Amit’s marks as 82.
This is useful when you discover mistakes in your data.
Adding a New Row
You may also need to add new records to a data frame.
Suppose a new student named Karan needs to be added.
You can use rbind():
new_student <- data.frame(
Name = “Karan”,
Age = 22,
Marks = 90
)
students <- rbind(students, new_student)
The new student will appear at the bottom of the data frame.
The rbind() function joins rows together. In simple terms, it allows you to place one row below another set of rows.
Removing a Column
Sometimes you may decide that a column is no longer needed.
For example, to remove the Passed column:
students$Passed <- NULL
The column will be removed from the data frame.
You can then check the data again by typing:
students
This is a simple way to clean up a table when you no longer need certain information.
Finding Students With High Marks
Data frames become especially useful when you want to find information that meets a condition.
Suppose you want to find students who scored more than 85.
You can write:
students[students$Marks > 85, ]
R checks the Marks column and keeps the rows where the marks are greater than 85.
This could return students such as Priya and Neha, depending on the current values in the data frame.
You can use similar conditions for other questions. For example, to find students who are 20 years old:
students[students$Age == 20, ]
The == sign means “is equal to.”
This kind of simple filtering is one of the reasons data frames are so useful when working with real information.
Sorting Data
You may also want to arrange students according to their marks.
For example:
students[order(students$Marks), ]
This arranges the rows from the lowest marks to the highest.
To arrange them from highest to lowest, you can use:
students[order(students$Marks, decreasing = TRUE), ]
Now the student with the highest marks will appear first.
This can be useful for sales data, exam results, product prices, or many other types of information.
Working With Missing Values
Real-world data is not always complete. Sometimes a value may be missing.
For example, imagine one student’s age was not entered:
students$Age[2] <- NA
Here, NA means that the value is missing.
You can check whether values are missing with:
is.na(students$Age)
R will show TRUE for the missing value and FALSE for values that are present.
Understanding missing values is important because real data often contains empty or unavailable information.
Data Frames and CSV Files
Data frames are not limited to information that you type directly into R. You can also load data from files.
A common file type is CSV, which stands for comma-separated values. It is widely used for storing table-like information.
Suppose you have a file called students.csv. You can read it into R with:
students <- read.csv(“students.csv”)
Once the file is loaded, students becomes a data frame that you can work with in the same way as the example we created earlier.
You can use head(students) to quickly check whether the file was loaded correctly.
This makes data frames very useful for everyday work because you can bring information from spreadsheets or other sources into R and then examine it.
Why Data Frames Matter in R
Data frames are important because much of the data people work with naturally fits into a table.
Customer information can be stored in rows and columns. Product details can be stored in the same way. School results, employee records, survey responses, website visits, and sales information can all be represented using data frames.
Once your information is inside a data frame, R gives you many simple ways to look at it, change it, search it, and calculate information from it.
The biggest advantage for beginners is that the basic idea is easy to understand. If you already understand how a table works, you already understand the main idea behind a data frame.
Final Thoughts
A data frame is simply one of the most useful ways to organize information in R. It works much like a table, with rows representing records and columns representing different types of information.
You can create a data frame with data.frame(), view it by typing its name, check its size with nrow() and ncol(), select columns with $, select rows with brackets, add new information, change existing values, remove columns, and find records that match a condition.
The best way to become comfortable with data frames is to practice with small examples. Start with a few rows and columns, change some values, add a column, and try selecting different parts of the table. Once these basic actions become familiar, working with larger sets of information in R will feel much easier.
Data frames may seem like a small part of R at first, but they form the foundation for many everyday tasks. Learning how they work is an important first step toward becoming comfortable with R and using it to work with real-world data.
