R Data Frame – Access Data by Row, Column, Name, and Condition
An R data frame is a two-dimensional table-like structure. Each row usually represents one record, and each column represents one variable. To access data from an R data frame, you can use square bracket indexing, column names with $, row and column positions, logical conditions, or helper functions such as subset(), rbind(), and cbind().
In this tutorial, we shall learn to access data of an R data frame, including selecting a single element, selecting rows, selecting columns, filtering rows by a column value, adding rows, adding columns, and deleting columns, with example R scripts.
We shall use an R data frame for examples with columns: name, age, and income.
Quick Reference for Accessing R Data Frame Values
The following table shows the common ways to read data from a data frame in base R.
| Task | Base R expression | Result |
|---|---|---|
| Get one element | df[2, 3] | Value at row 2, column 3 |
| Get one column as a vector | df$age | Values from the age column |
| Get one column as a data frame | df["age"] | One-column data frame |
| Get multiple columns | df[c("name", "income")] | Selected columns as a data frame |
| Get selected rows | df[c(2, 5), ] | Rows 2 and 5, all columns |
| Filter rows by condition | df[df$age == 29, ] | Rows where age is 29 |
| Exclude one column | df["income"] <- NULL | Removes the income column |
We shall look into following items to access meta information and data of an R Data Frame :
- Get Element at (i,j) ith row, jth column
- Extract column(s) of Data Frame
- Add row(s) to R Data Frame
- Add column(s) to R Data Frame
- Delete column(s) of R Data Frame
Get Element of R Data Frame by Row and Column Index
To extract an element or a block of elements from the ith row and jth column of an R data frame, use square bracket indexing. The part before the comma selects rows, and the part after the comma selects columns.
dataframe[row_numbers, column_numbers]
For example, dataframe[2, 3] returns the value at row 2 and column 3. If you pass more than one row or column number with c(), R returns a smaller data frame containing those rows and columns.
Example: access name and income values from selected rows
In this example, we initialize a data frame and access elements using row and column numbers.
r_df_get_element.R
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
# get elements from rows(2,5), columns(1,3)
elements = celebrities[c(2,5),c(1,3)]
print(elements)
Output
$ Rscript r_df_get_element.R
name income
2 Mathew 10.5
5 John 44.0
Access a single data frame value without reducing structure
When you request one row and one column, R returns a single value. When you request one column by name using single square brackets, R keeps the result as a data frame. This difference is useful when writing code that should preserve data frame structure.
# single value
dataframe[2, "income"]
# one-column data frame
dataframe["income"]
# vector from one column
dataframe[["income"]]
Select Rows from an R Data Frame by Row Number
To select complete rows, place the row numbers before the comma and leave the column side empty. The empty column side tells R to return all columns.
dataframe[row_numbers, ]
The following example returns the second, fifth, and seventh rows from the celebrities data frame.
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
selectedRows = celebrities[c(2, 5, 7), ]
print(selectedRows)
name age income
2 Mathew 23 10.5
5 John 38 44.0
7 Monica 29 45.0
Filter R Data Frame Rows by Column Value
To select rows that satisfy a condition, write a logical expression using a column of the data frame. The expression returns TRUE for rows to keep and FALSE for rows to remove.
dataframe[dataframe$column_name == value, ]
In the following example, we select all rows where age is equal to 29.
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
age29 = celebrities[celebrities$age == 29, ]
print(age29)
name age income
4 Philip 29 21.9
7 Monica 29 45.0
You can also combine conditions with & for AND and | for OR.
# rows where age is 29 and income is greater than 30
dataframe[dataframe$age == 29 & dataframe$income > 30, ]
# rows where age is 23 or income is greater than 40
dataframe[dataframe$age == 23 | dataframe$income > 40, ]
Extract Column or Multiple Columns of an R Data Frame
To extract some of the columns from a R Data Frame, call data.frame() function and provide the required columns in the data frame as arguments.
The syntax to extract columns of a data frame is
data.frame(dataframe$column_name_1, dataframe$column_name_2>)
You may select one or more columns from a data frame. If you are selecting multiple columns, use a comma separated list. Please observe that to select a column, we use dataframe followed by $ symbol followed by the required column name.
We may write the result to a new Data Frame or overwrite the original data frame.
A common base R alternative is to select column names inside square brackets. This is often cleaner because the output keeps the original column names.
dataframe[c("column_name_1", "column_name_2")]
Example: extract age and income columns from a data frame
In this example, we initialize a data frame and access its columns.
r_df_extract_columns.R
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
# extract columns (age, income)
extractedDF = data.frame(celebrities$age, celebrities$income)
# print to output
print("New data frame with two columns extracted from celebrities : ")
print(extractedDF)
Output
$ Rscript r_df_extract_columns.R
[1] "New data frame with two columns extracted from celebrities : "
celebrities.age celebrities.income
1 28 25.2
2 23 10.5
3 49 11.0
4 29 21.9
5 38 44.0
6 23 11.5
7 29 45.0
Example: extract data frame columns by column names
The following example selects the same two columns using their names. The result keeps the column names age and income.
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
extractedDF = celebrities[c("age", "income")]
print(extractedDF)
age income
1 28 25.2
2 23 10.5
3 49 11.0
4 29 21.9
5 38 44.0
6 23 11.5
7 29 45.0
Add Row or Rows to an R Data Frame with rbind()
To add more rows to an R Data Frame with the rows from other Data Frame, call rbind() function, and pass the original and other data frames as arguments to the function.
The syntax to call rbind() function is
resulting_data_frame = rbind(<existing_data_frame_name>,<additional_data_frame_name>)
For rbind() to work cleanly, the additional data frame should have matching column names and compatible data types. If a column is missing or named differently, R may return an error or create an unexpected result.
Example: append three records to a data frame
In this example, we initialize a data frame and add some more rows to it.
r_df_add_row.R
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
new_data = data.frame(name = c("Gary", "Lee", "Scofield"),
age = c(29, 22, 33),
income = c(21, 5, 31))
# add rows of new_data to celebrities
celebrities = rbind(celebrities, new_data)
# print to output
print("Resulting celebrities data frame with three newly added rows : ")
print(celebrities)
Output
$ Rscript r_df_add_row.R
[1] "Resulting celebrities data frame with three newly added rows : "
name age income
1 Andrew 28 25.2
2 Mathew 23 10.5
3 Dany 49 11.0
4 Philip 29 21.9
5 John 38 44.0
6 Bing 23 11.5
7 Monica 29 45.0
8 Gary 29 21.0
9 Lee 22 5.0
10 Scofield 33 31.0
Add Column or Columns to an R Data Frame
Following R function is used to add more columns to an R Data Frame.
resulting_data_frame = rbind(<existing_data_frame_name>,<new_data_frame_name>)
To add columns in base R, assign a vector to a new column name using $, or use cbind() when you want to bind one or more columns at once. The new column must have the same number of values as the number of rows in the data frame, unless a shorter value is intentionally recycled.
# add one column using $
dataframe$new_column = values
# add one or more columns using cbind()
dataframe = cbind(dataframe, new_columns)
Example: add a car column to an R data frame
In this example, we initialize a data frame and add some more columns to it.
r_df_add_column.R
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
# add column to the celebrities data frame
celebrities$car = c("Audi","Toyota","Bugati","Audi","Agera R","Bugati","Audi")
# print to output
print("Resulting celebrities data frame with newly added column : ")
print(celebrities)
Output
$ Rscript r_df_add_column.R
[1] "Resulting celebrities data frame with newly added column : "
name age income car
1 Andrew 28 25.2 Audi
2 Mathew 23 10.5 Toyota
3 Dany 49 11.0 Bugati
4 Philip 29 21.9 Audi
5 John 38 44.0 Agera R
6 Bing 23 11.5 Bugati
7 Monica 29 45.0 Audi
Delete Column or Columns from an R Data Frame
To delete a column from R Data Frame, you may select the columns you want to keep using extract columns of data frame and overwrite the existing data frame.
You can also remove a data frame column by assigning NULL to that column. This is a direct method when you know the column name to delete.
dataframe$column_name = NULL
The following example removes the income column from the celebrities data frame.
celebrities = data.frame(name = c("Andrew", "Mathew", "Dany", "Philip", "John", "Bing", "Monica"),
age = c(28, 23, 49, 29, 38, 23, 29),
income = c(25.2, 10.5, 11, 21.9, 44, 11.5, 45))
celebrities$income = NULL
print(celebrities)
name age
1 Andrew 28
2 Mathew 23
3 Dany 49
4 Philip 29
5 John 38
6 Bing 23
7 Monica 29
Common Mistakes When Accessing R Data Frame Data
- Forgetting the comma in square brackets:
dataframe[2]selects the second column, whiledataframe[2, ]selects the second row. - Using the wrong column name:
dataframe$Ageanddataframe$ageare different because R is case-sensitive. - Expecting
$to select multiple columns: usedataframe[c("age", "income")]for multiple columns. - Adding a column with the wrong length: the new vector should match the number of rows in the data frame.
- Mixing row binding and column binding: use
rbind()to add rows andcbind()or$assignment to add columns.
QA Checklist for R Data Frame Access Examples
- Check that every row-and-column example uses the form
dataframe[rows, columns]. - Confirm that column extraction examples show both vector output with
$or[[ ]]and data frame output with[ ]. - Verify that row filtering examples include the comma before the closing bracket, such as
dataframe[condition, ]. - Ensure that
rbind()examples add rows with matching column names. - Ensure that new columns have values for all rows in the example data frame.
FAQs on Accessing Data in an R Data Frame
How do I access a single value in an R data frame?
Use square brackets with a row index and a column index, such as dataframe[2, 3]. You can also use a column name, such as dataframe[2, "income"].
How do I select an entire column from an R data frame?
Use dataframe$column_name to return the column as a vector. Use dataframe["column_name"] if you want the result to remain a one-column data frame.
How do I select multiple columns in an R data frame?
Use a character vector of column names inside square brackets, such as dataframe[c("age", "income")]. This returns a data frame with the selected columns.
How do I filter rows in an R data frame by a condition?
Write the condition before the comma in square brackets. For example, dataframe[dataframe$age == 29, ] returns all rows where the age column is equal to 29.
What is the difference between rbind() and cbind() for data frames in R?
rbind() adds rows to a data frame, while cbind() adds columns. Use rbind() when the new data has the same columns, and use cbind() or $ assignment when adding new variables.
Conclusion: Accessing Rows, Columns, and Values in R Data Frames
In this R Tutorial, we have learnt to Access Data of R Data Frame like selecting rows, selecting columns, selecting rows that have a given column value, etc., with Example R Scripts. The key idea is to remember the square bracket pattern dataframe[rows, columns]: rows go before the comma, columns go after the comma, and leaving either side empty means all rows or all columns.
TutorialKart.com