Get and Set Row Names for Data Frames
Last Updated :
24 Apr, 2025
In this article, we will explore various methods to Get and Set row names for Data Frames in R Programming Language.
What is Data Frames?
Dataframes are the 2- dimensional data structure which organizes the data into rows and columns. These data frames are commonly used for manipulation, analysis, and visualization of data efficiently. Dataframes can have the ability to store different data types such as integers, characters, floating-point values ... etc. By default, the indexes or row names start from 0 in the data frames. Size of the data frames are mutable i.e. changes done at any time.
- Get the Row names
- Set the Row names
To Get the Row names
By default, every row name or index starts from 1 in the data frames which should be unique. By getting the row names in a data frame, we have some inbuilt functions or methods such as 'dimnames()', 'rownames()'. So by using these inbuilt functions, we can work more efficiently. For getting row names here, we are using some methods are
- dimnames()
- rownames()
Getting row names by using dimnames()
The dimnames() command is used to set or query the row and column names of a dataframe.Unlike rownames() or colnames() the dimnames() command operates both rows and columns. In Another way , Using ' dimnames() ' we are finding the names of each rows.
Syntax
dimnames(dataframe)
Here, created a dataframe by using 'data.frame()'. Using the function 'dimnames(df)[[1]]' , accessing all row names which were present in the dataframe.
R
df <- data.frame(
A = c(9, 8, 5),
B = c(4, 5, 6),
C = c(3,6,9)
)
print("The dataframe is:")
print(df)
# dimnames(df)[[1]] used for accessing row names
row_names <- dimnames(df)[[1]]
# Print row names
print("The row names is:")
print(row_names)
Output:
[1] "The dataframe is:"
A B C
1 9 4 3
2 8 5 6
3 5 6 9
[1] "The row names is:"
[1] "1" "2" "3"
Here, created a dataframe. Using the function 'dimnames(df)[[1]]' , accessing all row names which were present in the dataframe.
R
df <- data.frame(
A = c(4:7),
B = c("a","b","c","d"),
C = c("gfg","www","glob","stat")
)
print("The dataframe is:")
print(df)
# dimnames(df)[[1]] used for accessing row names
row_names <- dimnames(df)[[1]]
# Print row names
print("The row names is:")
print(row_names)
Output:
[1] "The dataframe is:"
A B C
1 4 a gfg
2 5 b www
3 6 c glob
4 7 d stat
[1] "The row names is:"
[1] "1" "2" "3" "4"
Getting row names by using rownames()
rownames() is used for accessing row names in the data frames. By default, every row names or indexes starts from 1. The function 'row names()' has ability to set and get row names in a dataframe. For getting the row names, rownames() function works more efficiently.
Syntax
rownames(dataframe)
Here, created a dataframe using different vectors. After,using the function 'rownames()' ,we find the row names.
R
#vector1
a=c("ck","dm","cd")
#vector2
b=c(1,2,3)
#vector3
c=c("kk","ll","mm")
print("Dataframe is:")
# creating dataframe
df=data.frame(a,b,c)
print(df)
#getting the row names
print("the row names is:")
print(rownames(df))
Output:
[1] "Dataframe is:"
a b c
1 ck 1 kk
2 dm 2 ll
3 cd 3 mm
[1] "the row names is:"
[1] "1" "2" "3"
Here ,created a dataframe By changing the default row names, we find row names using the function 'rownames()'.
R
df <- data.frame(A = c("a", "b", "c"),
B = c(11:13),
C = c(6, 7, 8))
print("The Original Dataframe : ")
print(df)
# changing the names of dataframe
rownames(df) <- c("G", "F", "H")
print(df)
# Finding the row names
rownames <- rownames(df)
print("The row names are: ")
print(rownames)
Output:
[1] "The Original Dataframe : "
A B C
1 a 11 6
2 b 12 7
3 c 13 8
A B C
G a 11 6
F b 12 7
H c 13 8
[1] "The row names are: "
[1] "G" "F" "H"
To Set the Row names
In Data frame's rows can be accessed by using the functions, such as rownames() ,row.names()..etc. By default, every row name or indexes starts from from 1 and should be unique.Set the row names mean removing default names and adding new names. For setting row names , we using some methods are.
- rownames()
- row.names()
Setting the row names using rownames()
rownames() is used for accessing row names in a data frame. By default, every row names or indexes starts from 1. The function 'rownames()' can have ability to set or add new names which were present in the dataframe.
Syntax
rownames(DataFrame)
Here, created a dataframe by using different vectors. After, by accessing a vector to the function 'rownames(df)' ,we changed the row names.
R
#vector1
a=c("ck","dm","cd")
#vector2
b=c(1,2,3)
#vector3
c=c("kk","ll","mm")
print("original dataframe:")
# creating dataframe
df=data.frame(a,b,c)
print(df)
#changing the row names
rownames(df)=c("A","B","C")
print("After Modifying the dataframe:")
print(df)
Output:
[1] "original dataframe:"
a b c
1 ck 1 kk
2 dm 2 ll
3 cd 3 mm
[1] "After Modifying the dataframe:"
a b c
A ck 1 kk
B dm 2 ll
C cd 3 mm
Setting the row names using row.names()
row.names() function can have ability to set or add new row names for the dataframes and works much similar to function 'rownames()' . The 'row.names()' is used for accessing the rows in efficient manner. By default row names or indexes starts from 1.
row.names(Dataframe)
Here, created a dataframe by using different vectors. After, by accessing a vector to the function'row.names(), we changed the row names.
R
a=c("cd","gfg","ww","globe")
b=c(1:4)
c=c("f","g","h","i")
print("original dataframe:")
# creating dataframe
df=data.frame(a,b,c)
print(df)
#changing the row names
row.names(df)=c("R1","R2","R3","R4")
print("After Modifying:")
print(df)
Output:
[1] "original dataframe:"
a b c
1 cd 1 f
2 gfg 2 g
3 ww 3 h
4 globe 4 i
[1] "After Modifying:"
a b c
R1 cd 1 f
R2 gfg 2 g
R3 ww 3 h
R4 globe 4 i
Conclusion
In conclusion,we learned two methods for getting row names and for setting row names by using the functions 'dimnames()' , 'rownames()' for getting and 'rownames()', 'row.names() for setting in a dataframe.
Similar Reads
Indexing and Slicing Data Frames in R
Indexing and Slicing are use for accessing and manipulating data.Indexing: Accessing specific elements (rows or columns) in data structures.Slicing: Extracting subsets of data based on conditions or indices.In R, indexing a data frame allows you to retrieve specific columns by their names:dataframeN
3 min read
How to get name of dataframe column in PySpark ?
In this article, we will discuss how to get the name of the Dataframe column in PySpark. To get the name of the columns present in the Dataframe we are using the columns function through this function we will get the list of all the column names present in the Dataframe. Syntax: df.columns We can a
3 min read
Merge two matrices by row names in R
In this article, we will examine various methods to merge two matrices by row names in the R programming language. What is a matrix?A matrix is defined as it is a two-dimensional data set which is the collection of rows and columns. A matrix can have the ability to contain or accept data of the same
4 min read
Set the First Column and Row as Index in Pandas
In Pandas, an index is a label that uniquely identifies each row or column in a DataFrame. Let's learn how to set the first column and row as index in Pandas DataFrame. Set First Column as Index in PandasConsider a Pandas DataFrame, to set the "Name" column as the index, use the set_index method: Py
3 min read
How to add header row to a Pandas Dataframe?
A header necessarily stores the names or headings for each of the columns. It helps the user to identify the role of the respective column in the data frame. The top row containing column names is called the header row of the data frame. There are two approaches to add header row to a Pandas Datafra
4 min read
Subset Dataframe Rows Based On Factor Levels in R
In this article, we will be discussing how to subset a given dataframe rows based on different factor levels with the help of some operators in the R programming language. Method 1: Subset dataframe Rows Based On One Factor Levels In this approach to subset dataframe rows based on one-factor levels,
2 min read
DataFrame Row Slice in R
In this article, we are going to see how to Slice row in Dataframe using R Programming Language. Row slicing in R is a way to access the data frame rows and further use them for operations or methods. The rows can be accessed in any possible order and stored in other vectors or matrices as well. Row
4 min read
Merge Several Data Frames into One Data Frame with a Loop in R
Merging several data frames into one is a common task in data analysis. Suppose you have multiple data frames with similar structures and you want to combine them into a single data frame. In that case, you can do this efficiently using a loop in R. This approach is beneficial when dealing with many
4 min read
DataFrame Rows & Column Segment in R
The process of extracting the row and column information in a dataset by simply using the index or slice operator is known as Slicing. In R Programming, the rows and columns can be sliced in two ways either by using an index or using the name of the row and column. The slice operator is much more us
2 min read
Get the specified row value of a given Pandas DataFrame
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Now let's see how to get the specified row value of a given DataFrame. We shall be using loc[ ], iloc[ ], and [ ] for a data frame object to select rows and col
2 min read