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"
Get and Set Row Names for Data Frames
In this article, we will explore various methods to Get and Set row names for Data Frames in R Programming Language.
Contact Us