Open In App

How to change Row Names of DataFrame in R ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The rows are stacked together, each denoted by a unique name. By default, the integer identifiers beginning from 1 to the number of rows are assigned to the data frame by default. The task here is to change the Rows names in given dataframe using R programming.

Dataset in use:

First  Second Third
1 a 7
2 ab 8
3 cv 9
4 dsd 10

Method 1 : using rownames()

A data frame’s rows can be accessed using rownames() method in the R programming language. We can specify the new row names using a vector of numerical or strings and assign it back to the rownames() method. The data frame is then modified reflecting the new row names. The number of items in the vector should be equivalent to the number of rows in the data frame. 

Syntax:

rownames(dataframe)

Example:

R




# declaring a data frame
df <- data.frame(First = c(1,2,3,4) , 
                 Second = c("a","ab","cv","dsd"),
                 Third=c(7:10))
  
# print original data frame
print ("Original DataFrame : ")
print (df)
  
# printing original rownames 
# of data frame
rownames <- rownames(df)
print ("Original row names ")
print (rownames)
  
# changing row names of data frame
rownames(df) <- c("Row1","Row2","Row3","Row4")
  
# print changed data frame
print ("Modified DataFrame : ")
print (df)


Output

[1] "Original DataFrame : "
 First Second Third
1     1      a     7
2     2     ab     8
3     3     cv     9
4     4    dsd    10
[1] "Original column names "
[1] "1" "2" "3" "4"
[1] "Modified DataFrame : "
    First Second Third
Row1     1      a     7
Row2     2     ab     8
Row3     3     cv     9
Row4     4    dsd    10

Method 2: using row.names()

There is an inbuilt R function, row.names() which can be used to access the row names of the data frame, and successively it can be modified using the new vector list.  The data frame is then modified reflecting the new row names.

Syntax:

row.names(dataframe)

R




# declaring a data frame
df <- data.frame(First = c(1,2,3,4) ,
                 Second = c("a","ab","cv","dsd"),
                 Third=c(7:10))
  
# print original data frame
print ("Original DataFrame : ")
print (df)
  
# printing original colnames
# of data frame
rownames <- rownames(df)
print ("Original row names ")
print (rownames)
  
# changing row names of data frame
row.names(df) <- LETTERS[1:4] 
  
# print changed data frame
print ("Modified DataFrame : ")
print (df)


Output

[1] "Original DataFrame : "
 First Second Third
1     1      a     7
2     2     ab     8
3     3     cv     9
4     4    dsd    10
[1] "Original row names "
[1] "1" "2" "3" "4"
[1] "Modified DataFrame : "
 First Second Third
A     1      a     7
B     2     ab     8
C     3     cv     9
D     4    dsd    10

Specific row names can also be modified by specifying the index of the row we wish to modify and then assigning it to a new object value. 

Example:

R




# declaring a data frame
df <- data.frame(First = c(1,2,3,4) ,
                 Second = c("a","ab","cv","dsd"),
                 Third=c(7:10))
  
# print original data frame
print ("Original DataFrame : ")
print (df)
  
# printing original colnames
# of data frame
rownames <- rownames(df)
print ("Original row names ")
print (rownames)
  
# changing row names of second
# row of data frame
row.names(df)[2] <- "ModifiedSecond"  
  
# print changed data frame
print ("Modified DataFrame : ")
print (df)


Output

[1] "Original DataFrame : "
 First Second Third
1     1      a     7
2     2     ab     8
3     3     cv     9
4     4    dsd    10
[1] "Original row names "
[1] "1" "2" "3" "4"
[1] "Modified DataFrame : "
              First Second Third
1                  1      a     7
ModifiedSecond     2     ab     8
3                  3     cv     9
4                  4    dsd    10


Last Updated : 28 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads