How to change Row Names of DataFrame in R ? - GeeksforGeeks

28 Apr, 2021
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:
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 framedf <- data.frame(First = c(1,2,3,4) ,                  Second = c("a","ab","cv","dsd"),                 Third=c(7:10))  # print original data frameprint ("Original DataFrame : ")print (df)  # printing original rownames # of data framerownames <- rownames(df)print ("Original row names ")print (rownames)  # changing row names of data framerownames(df) <- c("Row1","Row2","Row3","Row4")  # print changed data frameprint ("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
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 framedf <- data.frame(First = c(1,2,3,4) ,                 Second = c("a","ab","cv","dsd"),                 Third=c(7:10))  # print original data frameprint ("Original DataFrame : ")print (df)  # printing original colnames# of data framerownames <- rownames(df)print ("Original row names ")print (rownames)  # changing row names of data framerow.names(df) <- LETTERS[1:4]   # print changed data frameprint ("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 framedf <- data.frame(First = c(1,2,3,4) ,                 Second = c("a","ab","cv","dsd"),                 Third=c(7:10))  # print original data frameprint ("Original DataFrame : ")print (df)  # printing original colnames# of data framerownames <- rownames(df)print ("Original row names ")print (rownames)  # changing row names of second# row of data framerow.names(df)[2] <- "ModifiedSecond"    # print changed data frameprint ("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
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Adding Text to Plots in R programming - text() and mtext () Function
Calculate Standard Error in R
Compute Cumulative Log Normal Probability Density in R Programming - plnorm() Function
Identify and Remove Duplicate Data in R
Specify Reference Factor Level in Linear Regression in R
How to Replace specific values in column in R DataFrame ?
Remove rows with NA in one column of R DataFrame
How to filter R DataFrame by values in a column?
Merge DataFrames by Column Names in R
Convert Matrix to Dataframe in R