Open In App

Convert Values in Column into Row Names of DataFrame in R

Last Updated : 16 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The row names in R are by default mapped to the row numbers, beginning with the integer value 1. The row names can be modified easily and reassigned to any possible string vector to assign customized names. Since, the row numbers are practically equal in each column of the dataframe, therefore the column values can also be assigned to the row names in R. 

Method 1 : Using rownames() method

The rownames() method in R is used to assign row names to the dataframe. It is assigned using a character vector consisting of desired names with a length equivalent to the number of rows in dataframe. We can simply assign it to any column of the dataframe if it contains all unique values. The column can be accessed using standard indexing methods, that is, df$col_name or df[,col_indx]. The changes are made to the original dataframe. row.names(df) can also be used instead of rownames() method. 

Syntax:

rownames(df)/row.names(df) <- df$col_name

rownames(df)/row.names(df) <- df[,col_indx]

Example:

R




data_frame <- data.frame(col1=letters[1:4],
                         col2=6,col3=c(5:8))
  
print("Original dataframe")
print(data_frame)
  
# reassigning row names
rownames(data_frame) <- data_frame$col1
print("Modified dataframe")
print(data_frame)


Output

[1] "Original dataframe"
 col1 col2 col3
1    a    6    5
2    b    6    6
3    c    6    7
4    d    6    8
[1] "Modified dataframe"
 col1 col2 col3
a    a    6    5
b    b    6    6
c    c    6    7
d    d    6    8

However, if we try to assign the col2 in this case as the row names of the dataframe, the R code throws an exception, because duplicate row names are not allowed, therefore an important condition is that the column values must be unique in order to assign it to the row names of the dataframe. 

Error in `.rowNamesDF<-`(x, value = value) :
 duplicate 'row.names' are not allowed
Calls: rownames<- ... row.names<- -> row.names<-.data.frame -> .rowNamesDF<-
In addition: Warning message:
non-unique value when setting 'row.names': ‘6’
Execution halted

Method 2 : Using tiddle

The tiddle package can be installed in the working space in order to perform the manipulation of row names of the dataframe. First, the remove_rownames method from this package is applied over the dataframe, which removes the reference of the existing row names, and then the column_to_rownames() method is invoked. It takes as an argument the column name whose values we wish to assign to the row names of the dataframe. These changes, however, have to be stored in the original dataframe, for further usage.

Example:

R




library(tidyverse)
  
data_frame <- data.frame(col1=letters[1:4],
                         col2=6,col3=c(5:8))
  
print("Original dataframe")
print(data_frame)
  
data_frame <- data_frame %>% remove_rownames %>% column_to_rownames(var="col1")
print("Modified dataframe")
print(data_frame)


Output

[1] "Original dataframe"
col1 col2 col3
1    a    6    5
2    b    6    6
3    c    6    7
4    d    6    8
[1] "Modified dataframe"
col1 col2 col3
a    a    6    5
b    b    6    6
c    c    6    7
d    d    6    8

Method 3: Using row.names as an argument of data.frame creation

A new dataframe can be created using the existing dataframe, which takes as argument , the row.names attribute mapped to the column whose values are mapped to the row names. However, this would lead to the deletion of that column from the dataframe and, the number of columns thereby will reduce by 1. All the other columns are shifted one index towards the left side. Therefore, this approach may lead to ambiguity in data. 

Example:

R




data_frame <- data.frame(col1=letters[1:4],
                         col2=6,col3=c(5:8))
print("Original dataframe")
print(data_frame)
  
# reassigning row names
data_frame <- data.frame(data_frame, row.names = 3)
print("Modified dataframe")
print(data_frame)


Output

[1] "Original dataframe"
 col1 col2 col3
1    a    6    5
2    b    6    6
3    c    6    7
4    d    6    8
[1] "Modified dataframe"
 col1 col2
5    a    6
6    b    6
7    c    6
8    d    6


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

Similar Reads