Open In App

Get and Set Row Names for Data Frames

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. Get the Row names
  2. 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

  1. dimnames()
  2. 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.

  1. rownames()
  2. 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads