R: How to Convert Values in Column to Row Names


Often you may want to convert the values in the first column of a data frame in R into row names.

You can use the following methods to do so:

Method 1: Convert Column Values to Row Names Using Base R

#use values from first column as row names
rownames(df) <- df[,1]

#remove first column from data frame
df <- df[,-1]

This particular example uses the values from the first column of the data frame as the rownames and then removes the first column from the data frame.

Method 2: Convert Column Values to Row Names Using tidyverse Package

library(tidyverse)

#use values from 'team' column as row names
df %>% remove_rownames %>% column_to_rownames(var='team')

This particular example uses functions from the tidyverse package in R to first remove any existing rownames from a data frame and then use the values from the team column as the new rownames.

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 45, 35, 34, 45, 28, 31))

#view data frame
df

  team points assists
1    A     99      22
2    B     68      28
3    C     86      45
4    D     88      35
5    E     95      34
6    F     74      45
7    G     78      28
8    H     93      31

Example 1: Convert Column Values to Row Names Using Base R

Suppose that we would like to convert the values from the team column of the data frame (A, B, C, etc.) into the rownames of the data frame.

We can use the following syntax to do so:

#use values from first column as row names
rownames(df) <- df[,1]

#remove first column from data frame
df <- df[,-1]

#view updated data frame
df

  points assists
A     99      22
B     68      28
C     86      45
D     88      35
E     95      34
F     74      45
G     78      28
H     93      31

Notice that the values from the team column are now used as the rownames in the data frame.

Also note that we used the syntax df <- df[, -1] to specify that we’d like to remove the first column from the data frame.

This ensures that the team column is no longer shown as the first column in the data frame.

Example 2:Convert Column Values to Row Names Using tidyverse Package

Suppose that we would like to convert the values from the team column of the data frame (A, B, C, etc.) into the rownames of the data frame by using functions from the tidyverse package.

We can use the following syntax to do so:

library(tidyverse)

#use values from 'team' column as row names
df %>% remove_rownames %>% column_to_rownames(var='team')

  points assists
A     99      22
B     68      28
C     86      45
D     88      35
E     95      34
F     74      45
G     78      28
H     93      31

Notice that the values from the team column are now used as the rownames in the data frame.

Note that this produces the same result as the previous example. In scenarios where you have a large data frame, this method will likely be faster than the method in base R.

Note: You can find the complete documentation for the column_to_rownames() function from the tidyverse package here.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Add Suffix to Column Names in R
How to Loop Through Column Names in R
How to Merge Data Frames by Column Names in R
How to Compare Three Columns in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *